Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 1997-2013, tinygroup.org (luo_guo@live.cn). 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. -------------------------------------------------------------------------- 版权 (c) 1997-2013, tinygroup.org (luo_guo@live.cn). 本开源软件遵循 GPL 3.0 协议; 如果您不遵循此协议,则不被允许使用此文件。 你可以从下面的地址获取完整的协议文本 http://www.gnu.org/licenses/gpl.html
 
 package org.tinygroup.mda.transform.view.impl;
 
 import java.util.Map;
 
 
 public class ViewModelTransformerImpl implements ViewModelTransformer {
 	private ViewModel model;
 	private String VIEW_PRE = "view_pre";
 
 		this. = model;
 	}
 
 	public ViewModel getModle() {
 		return ;
 	}
 
 	public View tranformToView(FileUtil fileUtil,Map<StringStringconfigMap) {
 		View tableView = new View();
 		String viewPre = "";
 		if(configMap!=null)
 			viewPre = configMap.get();
 		tableView.setName(MDANameUtil.getViewName(,viewPre));
 		Map<StringViewTabletableMap = new HashMap<StringViewTable>();
 		Map<StringViewFieldmap = new HashMap<StringViewField>();
 		dealRelateModels(tableViewtableMap);
 		dealModelRelations(tableViewtableMap);
 		dealFields(tableViewmap);
 		dealOrderByField(tableViewmaptableMap);
 		// dealGroupByField(tableView, map);
 		dealConditionFields(tableViewmaptableMap);
 		fileUtil.writeViewFile(tableView);
 		return tableView;
 	}
 
 	private void dealConditionFields(View tableView,
 			Map<StringViewFieldmapMap<StringViewTabletableMap) {
 		for (ConditionField field : .getConditionFields()) {
 			if (field.getAggregateFunction() == null
 					|| field.getAggregateFunction().equals("")) {
 				// 无聚合函数配置则声称where条件
 				conditionFieldToCondition(tableViewfieldmaptableMap);
 			} else { // 有则生成having语句
 				conditionFieldToConditionHaving(tableViewfieldmaptableMap);
 			}
 
 		}
 	}
 
	private void conditionFieldToConditionHaving(View tableView,
			Map<StringViewTabletableMap) {
		ViewHaving having = new ViewHaving();
		// 处理keyField;
		Having keyHaving = new Having();
		if (field.getModeluuid() == null) {
			ViewFieldRef vf = new ViewFieldRef();
			keyHaving.setField(vf);
else {
			ViewFieldRef vf = new ViewFieldRef();
			vf.setViewTableId(tableMap.get(field.getModeluuid()).getId());
			keyHaving.setField(vf);
		}
		// TODO:operator这里要修改
		having.setOperator(field.getCompareMode());
		having.setKeyHaving(keyHaving);
		ConditionValue value = field.getValue();
		if (value.getConstValue() == null) {
			Having valueHaving = new Having();
			ViewFieldRef vf = new ViewFieldRef();
			vf.setViewTableId(tableMap.get(value.getModeluuid()).getId());
			valueHaving.setField(vf);
			having.setValueHaving(valueHaving);
else {
			having.setValue(value.getConstValue());
		}
		tableView.getHavingList().add(having);
	}
	private void conditionFieldToCondition(View tableView,
			Map<StringViewTabletableMap) {
		ViewCondition condition = new ViewCondition();
		// 处理keyField;
		if (field.getModeluuid() == null) {
			ViewFieldRef vf = new ViewFieldRef();
			condition.setKeyField(vf);
else {
			ViewFieldRef vf = new ViewFieldRef();
			vf.setViewTableId(tableMap.get(field.getModeluuid()).getId());
			condition.setKeyField(vf);
		}
		// TODO:operator这里要修改
		condition.setOperator(field.getCompareMode());
		// 条件值一定是固定值或者是一个字段
		// 处理valueField;
		ConditionValue value = field.getValue();
		if (value.getConstValue() != null) {
			condition.setValue(value.getConstValue());
else {
			ViewFieldRef vf = new ViewFieldRef();
			vf.setViewTableId(tableMap.get(value.getModeluuid()).getId());
			condition.setValueField(vf);
		}
		tableView.getConditionList().add(condition);
	}
	// private void dealGroupByField(View tableView, Map<String, ViewField> map)
	// {
	// // 设置分组字段
	// for (GroupField field : model.getGroupFields()) {
	// GroupByField groupByField = new GroupByField();
	// if (field.getModeluuid() == null) {// 如果模型id为空,则代表为当前模型字段
	// groupByField.setField(map.get(field.getFielduuid()));
	// } else {
	// groupByField.setField(getViewFieldRef(field.getModeluuid(),
	// field.getFielduuid()));
	// }
	// tableView.getGroupByFieldList().add(groupByField);
	// }
	// }
	private void dealOrderByField(View tableViewMap<StringViewFieldmap,
			Map<StringViewTabletableMap) {
		// 设置排序字段
		for (OrderField orderField : .getOrderFields()) {
			OrderByField orderByField = new OrderByField();
			if (orderField.getModeluuid() == null) {// 如果模型id为空,则代表为当前模型字段
				ViewFieldRef vf = new ViewFieldRef();
				vf.setViewFieldId(map.get(orderField.getFielduuid()).getId());
				orderByField.setField(vf);
else {
						orderField.getModeluuid(), orderFieldtableMap));
			}
			orderByField.setDirection(orderField.getOrderMode());
			tableView.getOrderByFieldList().add(orderByField);
		}
	}

根据 关联模型id与关联模型字段id生成一个ViewField 该场景主要应用在 where/orderby/groupby/having等子句中出现了某个不存在于视图模型字段列表中的字段

Parameters:
relateModeluuid
modelFielduuid
Returns:
	private ViewFieldRef getViewFieldRef(String relateModeluuid,
			OrderField orderFieldMap<StringViewTabletableMap) {
		String modelFielduuid = orderField.getFielduuid();
		ViewFieldRef vf = new ViewFieldRef();
		RelateModel model = getRelateModel(relateModeluuid);
		// EntityModel entity = ModelManagerFactory.getManager().getEntityModel(
		// model.getEntityuuid());
		// EntityField entityField = MDAUtil.getField(entity, modelFielduuid);
		vf.setTableFieldId(modelFielduuid);
		// (getTableName(model));
		vf.setViewTableId((tableMap.get(model.getUuid()).getId()));
		return vf;
	}
	private void dealFields(View tableViewMap<StringViewFieldmap) {
		// 生成视图字段
		for (ViewModelField field : MDAUtil.getViewModelFields()) {
			RelateModel relatemodel = MDAUtil.getRelateModel(,
			EntityModel entity = ModelManagerFactory.getManager()
			EntityField entityField = MDAUtil.getField(entity,
			ViewField vf = new ViewField();
			vf.setAlias(field.getAlias());
			vf.setTableFieldId(entityField.getUuid());
			vf.setId(field.getUuid());
			vf.setViewTable(relatemodel.getUuid());
			tableView.getFieldList().add(vf);
			map.put(field.getUuid(), vf);
		}
	}
	private void dealModelRelations(View tableView,
			Map<StringViewTabletableMap) {
		// 模型的模型关系列表,生成为view的条件列表
		for (ModelRelation relation : .getRelations()) {
			dealModelRelation(tableViewrelationtableMap);
		}
	}
	private void dealModelRelation(View tableViewModelRelation relation,
			Map<StringViewTabletableMap) {
		EntityModel entity = ModelManagerFactory.getManager().getEntityModel(
		EntityModel entity2 = ModelManagerFactory.getManager().getEntityModel(
		// 拼装 entity.xxx=
		ViewCondition condition = new ViewCondition();
		condition.setOperator("=");
		ViewFieldRef keyField = new ViewFieldRef();
		keyField.setViewTableId(tableMap.get(rm.getUuid()).getId());
		EntityField foreignField = null;
		for (EntityField field : MDAUtil.getFields(entity)) {
			if (rm2.getEntityuuid().equals(field.getEntityuuid())) { // 如果是那个外键
				foreignField = field;
				break;
			}
		}
		// foreignField不可能为空
		keyField.setTableFieldId(foreignField.getUuid());
		condition.setKeyField(keyField);
		// 拼装entity2.id
		ViewFieldRef valueField = new ViewFieldRef();
		valueField.setViewTableId(tableMap.get(rm2.getUuid()).getId());
		valueField.setTableFieldId(MDAUtil.getPrimarykey(entity2).getUuid());
		// 生成条件
		// entity.xxx=entity2.id
		condition.setValueField(valueField);
		tableView.getConditionList().add(condition);
	}
	private void dealRelateModels(View tableView,
			Map<StringViewTabletableMap) {
		// 模型的关联模型列表,生成为view的关联表
		for (RelateModel relateModel : .getRelateModels()) {
			String entityuuid = relateModel.getEntityuuid();
			String alias = relateModel.getEntityalias();
			EntityModel entity = ModelManagerFactory.getManager()
					.getEntityModel(entityuuid);
			ViewTable vt = createViewTable(aliasrelateModelentity);
			tableMap.put(relateModel.getUuid(), vt);
			tableView.getTableList().add(vt);
		}
	}
	// private String getTableName(RelateModel model) {
	// if (model.getEntityalias() != null
	// || !model.getEntityalias().equals(""))
	// return model.getEntityalias();
	// EntityModel entity = ModelManagerFactory.getManager().getEntityModel(
	// model.getEntityuuid());
	// return entity.getName();
	//
	// }
	private ViewTable createViewTable(String aliasRelateModel relateModel,
			EntityModel entity) {
		ViewTable vt = new ViewTable();
		vt.setId(relateModel.getUuid());
		vt.setTableAlias(alias);
		vt.setTableId(MDANameUtil.getTableId(entity));
		return vt;
	}
	private RelateModel getRelateModel(String uuid) {
		for (RelateModel rm : .getRelateModels()) {
			if (rm.getUuid().equals(uuid)) {
				return rm;
			}
		}
		return null;
	}
		BeanDetial beanDetial = new BeanDetial();
		beanDetial.setName(MDANameUtil.getBeanName(.getName()));
		for (ViewModelField field : MDAUtil.getFields()) {
			String entityFieldUUID = field.getEntityFieldduuid();
			String relateModelUUID = field.getRelateModelUuid();
			RelateModel relateModel = getRelateModel(relateModelUUID);
			EntityModel entityModel = ModelManagerFactory.getManager()
			EntityField entityField = MDAUtil.getField(entityModel,
					entityFieldUUID);
			// beanDef.addProperty(entityField.getStandardFieldId());
			String stduuid = entityField.getStandardFieldId();
			StandardField propertyField = MDAUtil.getField(stduuid);
			BeanProperty beanProperty = new BeanProperty();
			beanProperty.setJavaType(MDAUtil.getFieldType(stduuid"java"));
			beanProperty.setName(MDANameUtil.getMDAName(
					propertyField.getName()));
			beanDetial.addProperty(beanProperty);
		}
		return beanDetial;
	}
New to GrepCode? Check out our FAQ X