/*
* 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 com.mongodb.DB;
import com.mongodb.DBRef;
import net.tooan.ynpay.third.jfinal.log.Logger;
import net.tooan.ynpay.third.mongodb.BuguConnection;
import net.tooan.ynpay.third.mongodb.annotations.Id;
import net.tooan.ynpay.third.mongodb.annotations.Ref;
import net.tooan.ynpay.third.mongodb.annotations.RefList;
import net.tooan.ynpay.third.mongodb.cache.FieldsCache;
import net.tooan.ynpay.third.mongodb.exception.DBConnectionException;
import net.tooan.ynpay.third.mongodb.exception.FieldException;
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 ReferenceUtil {
private final static Logger logger = Logger.getLogger(ReferenceUtil.class);
public static Object toDbReference(Ref ref, Class<?> clazz, String idStr) {
if (StringUtil.isEmpty(idStr)) {
return null;
}
Object result = null;
if (ref.reduced()) {
result = toManualRef(clazz, idStr);
} else {
result = toDBRef(clazz, idStr);
}
return result;
}
public static Object toDbReference(RefList refList, Class<?> clazz, String idStr) {
if (StringUtil.isEmpty(idStr)) {
return null;
}
Object result = null;
if (refList.reduced()) {
result = toManualRef(clazz, idStr);
} else {
result = toDBRef(clazz, idStr);
}
return result;
}
private static Object toManualRef(Class<?> clazz, String idStr) {
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;
}
private static DBRef toDBRef(Class<?> clazz, String idStr) {
DB db = null;
String[] name = MapperUtil.getEntityName(clazz);
try {
db = BuguConnection.getInstance().getDB(name[0]);
} catch (DBConnectionException ex) {
logger.error(ex.getMessage(), ex);
}
Object dbId = IdUtil.toDbId(clazz, idStr);
return new DBRef(db, name[1], dbId);
}
public static String fromDbReference(Ref ref, Object value) {
String result = null;
if (ref.reduced()) {
result = value.toString();
} else {
DBRef dbRef = (DBRef) value;
result = dbRef.getId().toString();
}
return result;
}
public static String fromDbReference(RefList refList, Object value) {
String result = null;
if (refList.reduced()) {
result = value.toString();
} else {
DBRef dbRef = (DBRef) value;
result = dbRef.getId().toString();
}
return result;
}
public static Object toDbReference(Class<?> clazz, String fieldName, Class<?> refClass, String idStr) {
Object result = null;
Field refField = null;
try {
refField = FieldsCache.getInstance().getField(clazz, fieldName);
} catch (FieldException ex) {
logger.error(ex.getMessage(), ex);
}
Ref ref = refField.getAnnotation(Ref.class);
if (ref != null) {
result = toDbReference(ref, refClass, idStr);
} else {
RefList refList = refField.getAnnotation(RefList.class);
result = toDbReference(refList, refClass, idStr);
}
return result;
}
}