/*
* Copyright (c) www.bugull.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.tooan.ynpay.third.mongodb.mapper;
import net.tooan.ynpay.third.jfinal.log.Logger;
import net.tooan.ynpay.third.mongodb.annotations.Id;
import net.tooan.ynpay.third.mongodb.cache.FieldsCache;
import net.tooan.ynpay.third.mongodb.exception.IdException;
import org.bson.types.ObjectId;
import java.lang.reflect.Field;
/**
* @author Frank Wen(xbwen@hotmail.com)
*/
public final class IdUtil {
private final static Logger logger = Logger.getLogger(IdUtil.class);
/**
* Convert the id string to object, which matching the id data in mongoDB.
*
* @param clazz
* @param idStr
* @return
*/
public static Object toDbId(Class<?> clazz, String idStr) {
if (StringUtil.isEmpty(idStr)) {
return null;
}
Object result = null;
Field idField = null;
try {
idField = FieldsCache.getInstance().getIdField(clazz);
} catch (IdException ex) {
logger.error(ex.getMessage(), ex);
}
Id idAnnotation = idField.getAnnotation(Id.class);
switch (idAnnotation.type()) {
case AUTO_GENERATE:
result = new ObjectId(idStr);
break;
case AUTO_INCREASE:
result = Long.parseLong(idStr);
break;
case USER_DEFINE:
result = idStr;
break;
}
return result;
}
}