Java tutorial
/** * Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com). * * Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html * * 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 org.tinygroup.mongodb.engine.operation; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import org.bson.BSONObject; import org.bson.types.BasicBSONList; import org.tinygroup.commons.tools.CollectionUtil; import org.tinygroup.context.Context; import org.tinygroup.imda.util.MdaUtil; import org.tinygroup.mongodb.common.*; import org.tinygroup.mongodb.engine.MongoDbContext; import org.tinygroup.mongodb.engine.MongoField; import org.tinygroup.mongodb.model.MongoDBModel; import org.tinygroup.mongodb.util.MapConvert; import org.tinygroup.weblayer.webcontext.parser.fileupload.TinyFileItem; import org.tinygroup.weblayer.webcontext.parser.impl.ItemFileObject; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; /** * * : ???? * <p> * ?: renhui <br> * ?: 2013-11-27 <br> * <br> */ public class MongoOperationContext extends MongoDbContext { private Operation operation; public MongoOperationContext(MongoDBModel model, Operation operation, Context context) { super(model, context); this.operation = operation; } /** * * ??? */ public BSONObject insertMongoModel() { if (operation == null) { return null; } DBObject insert = new BasicDBObject(); insertOperationObject(operation.getOperationGroup(), insert); return persistence.insert(insert); } /** * * ??? */ public BSONObject addObjectArrayModel() { if (operation != null) { DBObject objectArray = new BasicDBObject(); DBObject common = new BasicDBObject(); addObjectArrayOperationObject(operation.getOperationGroup(), objectArray, common); DBObject condition = generateConditionObject(); if (common.keySet().size() > 0) { persistence.update(condition, common);// } return persistence.insertObjectArrayModel(condition, objectArray);//? } return null; } /** * * ?? */ public BSONObject removeObjectModel() { if (operation != null) { DBObject update = new BasicDBObject(); removeObjectOperationObject(operation.getOperationGroup(), update); DBObject condition = generateConditionObject(); return persistence.removeObjectModel(condition, update); } return null; } /** * * ?? * * @return */ public Long updateMongoModel() { if (operation != null) { DBObject update = new BasicDBObject(); updateOperationObject(operation.getOperationGroup(), update); DBObject condition = generateConditionObject(); return persistence.update(condition, update); } return null; } public Long deleteMongoModel() { if (operation != null) { DBObject condition = generateConditionObject(); // ? return persistence.delete(condition); } return null; } public BSONObject[] viewMongoModel() { if (operation != null) { DBObject selectObject = generateOperationSelectObject(); DBObject conditionObject = generateConditionObject(); // ? return persistence.find(selectObject, conditionObject); } return null; } /** * * ???bsonobject * * @return */ public BSONObject createBSONObject() { return generateConditionObject(); } /** * * ??? * * @return */ public Context buildOperationFieldParamter(OperationGroup operationGroup, Context newContext) { if (operationGroup != null) { List<OperationField> operationFields = operationGroup.getFields(); for (OperationField operationField : operationFields) { MongoField mongoField = getMongoField(operationField.getFieldId()); Field field = mongoField.getField(); String propertyName = mongoField.getFieldName(); Object value = context.get(propertyName); if (value == null) {//mongoField.getFieldName()?aaa.name,???name? value = context.get(field.getName()); } if (value != null) { if (value.getClass().isArray()) {//???? Object[] values = (Object[]) value; for (int i = 0; i < values.length; i++) { if (values[i] instanceof ItemFileObject) { ItemFileObject fileObject = (ItemFileObject) values[i]; TinyFileItem fileItem = (TinyFileItem) fileObject.getFileItem(); values[i] = fileItem.getUnique(); } } } else { if (value instanceof ItemFileObject) {//? ItemFileObject fileObject = (ItemFileObject) value; TinyFileItem fileItem = (TinyFileItem) fileObject.getFileItem(); value = fileItem.getUnique(); } } if (field.isEncrypt()) {//? try { value = cryptor.encrypt(String.valueOf(value)); } catch (Exception e) { logger.errorMessage("encrypt error", e); } } } else {//?null??? if (mongoField.isObjectIdField()) { value = UUID.randomUUID().toString().replaceAll("-", ""); } } //??? newContext.put(propertyName, MdaUtil.getObject(value, field.getDefaultValue(), field.getDataType(), field.getDefaultValueGetter())); } if (operationGroup.getOperationGroups() != null) { for (OperationGroup subGroup : operationGroup.getOperationGroups()) { buildOperationFieldParamter(subGroup, newContext); } } } return newContext; } /** * * ??? * * @return */ public Context buildAllParamter(Context newContext) { buildConditionFieldParamter(operation.getConditionFields(), newContext); buildOperationFieldParamter(operation.getOperationGroup(), newContext); return newContext; } /** * * ???BSONObject * * @return */ public BSONObject generateOperationObject() { BSONObject operationObject = new BasicDBObject(); initOperationObject(operation.getOperationGroup(), operationObject); return operationObject; } /** * * ??BSONObject * * @return */ public DBObject generateOperationSelectObject() { DBObject operationObject = new BasicDBObject(); initOperationSelectObject(operation.getOperationGroup(), operationObject); return operationObject; } private void initOperationSelectObject(OperationGroup group, BSONObject bsonObject) { List<OperationField> operationFields = group.getFields(); for (OperationField operationField : operationFields) { if (isCollectionField(operationField.getFieldId())) { MongoField mongoField = getMongoField(operationField.getFieldId()); String propertyName = mongoField.getFieldName(); bsonObject.put(propertyName, 1);// ?? // bsonObject = propertyCycleSet(bsonObject, field.getName()); } } if (!CollectionUtil.isEmpty(group.getOperationGroups())) { for (OperationGroup operationGroup : group.getOperationGroups()) { initOperationSelectObject(operationGroup, bsonObject); } } } /** * * ???BSONObject * * @return */ public DBObject generateConditionObject() { return generateConditionObject(operation.getConditionFields()); } private void initOperationObject(OperationGroup group, BSONObject bsonObject) { MapConvert convert = new MapConvert(model, operation, context); Map<String, Object> map = new HashMap<String, Object>(); convert.initOperationObject(operation.getOperationGroup(), map); bsonObject.putAll(map); } private void addObjectArrayOperationObject(OperationGroup group, BSONObject objectArrayObject, BSONObject commonObject) { MapConvert convert = new MapConvert(model, operation, context); Map<String, Object> commonMap = new HashMap<String, Object>(); Map<String, Object> addToSetMap = new HashMap<String, Object>(); convert.addObjectArrayOperationObject(operation.getOperationGroup(), addToSetMap, commonMap); objectArrayObject.putAll(addToSetMap); commonObject.putAll(commonMap); } private void removeObjectOperationObject(OperationGroup group, BSONObject bsonObject) { MapConvert convert = new MapConvert(model, operation, context); Map<String, Object> map = new HashMap<String, Object>(); convert.removeObjectOperationObject(operation.getOperationGroup(), map); bsonObject.putAll(map); } private void insertOperationObject(OperationGroup group, BSONObject bsonObject) { MapConvert convert = new MapConvert(model, operation, context); Map<String, Object> map = new HashMap<String, Object>(); convert.insertOperationObject(operation.getOperationGroup(), map); bsonObject.putAll(map); } private void updateOperationObject(OperationGroup group, BSONObject bsonObject) { MapConvert convert = new MapConvert(model, operation, context); Map<String, Object> map = new HashMap<String, Object>(); convert.updateOperationObject(operation.getOperationGroup(), map); bsonObject.putAll(map); } // private BSONObject propertyCycleSet( // BSONObject bsonObject, MongoField mongoField) { // String propertyName=mongoField.getFieldName(); // String tempName = propertyName; // int index = tempName.indexOf("."); // if (index != -1) { // do { // String key = tempName.substring(0, index); // String nextPropertyName = tempName.substring(index + 1, // tempName.length()); // BSONObject subObject = createSubObject(key, bsonObject); // index = nextPropertyName.indexOf("."); // if (index == -1) { // mongoField.fieldValueSet(bsonObject, context); // } // tempName = nextPropertyName; // bsonObject = subObject; // } while (index != -1); // // } else { // bsonObject.put(propertyName, context.get(propertyName)); // } // return bsonObject; // } // // private BSONObject createSubObject(String key, // BSONObject bsonObject) { // BSONObject subObject = (BSONObject) bsonObject.get(key); // ObjectField objectField = model.getObjectField(key); // if (subObject == null) { // if(objectField!=null){ // int size = objectField.getArraySize(); // if (size > 1) { // subObject = new BasicBSONList(); // BasicBSONList list=(BasicBSONList)subObject; // for (int i = 0; i < size; i++) { // BSONObject object = new BasicDBObject(); // List<Field> fields=objectField.getFields(); // for (Field field : fields) { // MongoField mongoField=model.getMongoField(field.getId()); // mongoField.fieldValueSet(i,object, context); // } // list.add(i, object); // } // subObject=list; // } else { // subObject=new BasicDBObject(); // } // bsonObject.put(key, subObject); // } // } // return subObject; // } public void propertyValueSet(ObjectField objectField, BSONObject bsonObject) { if (objectField != null) { int size = objectField.getArraySize(); if (size > 1) { BasicBSONList list = (BasicBSONList) bsonObject.get(objectField.getName()); if (list == null) { list = new BasicBSONList(); for (int i = 0; i < size; i++) { BSONObject object = objectFieldSet(objectField, i); list.add(i, object); } bsonObject.put(objectField.getName(), list); } } else { BSONObject object = (BSONObject) bsonObject.get(objectField.getName()); if (object == null) { object = objectFieldSet(objectField, 0); bsonObject.put(objectField.getName(), object); } } } } private BSONObject objectFieldSet(ObjectField objectField, int j) { BSONObject object = new BasicDBObject(); List<Field> fields = objectField.getFields(); for (Field field : fields) { MongoField mongoField = model.getMongoField(field.getId()); if (!mongoField.isHasSetValue()) { mongoField.fieldValueSet(j, object, context); mongoField.setHasSetValue(true); } } List<ObjectField> objectFields = objectField.getObjectFields(); for (ObjectField objectField2 : objectFields) { propertyValueSet(objectField2, object); } return object; } }