ActionCreate.java 4.8 KB
Newer Older
caixiangyi's avatar
caixiangyi 已提交
1 2 3 4 5 6 7 8 9
package com.x.query.assemble.designer.jaxrs.input;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.JsonElement;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.entity.JpaObject;
R
update  
roo00 已提交
10
import com.x.base.core.project.cache.ApplicationCache;
O
o2sword 已提交
11
import com.x.base.core.project.cache.CacheManager;
caixiangyi's avatar
caixiangyi 已提交
12 13 14 15 16 17 18 19 20 21
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.jaxrs.WoId;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.query.assemble.designer.Business;
import com.x.query.core.entity.Query;
import com.x.query.core.entity.Reveal;
import com.x.query.core.entity.Stat;
import com.x.query.core.entity.View;
R
update  
roo00 已提交
22 23 24
import com.x.query.core.entity.schema.Statement;
import com.x.query.core.entity.schema.Table;
import com.x.query.core.entity.wrap.*;
caixiangyi's avatar
caixiangyi 已提交
25 26 27 28 29 30 31 32 33 34 35 36

class ActionCreate extends BaseAction {

	private static Logger logger = LoggerFactory.getLogger(ActionCreate.class);

	ActionResult<Wo> execute(EffectivePerson effectivePerson, JsonElement jsonElement) throws Exception {
		logger.debug(effectivePerson, "jsonElement:{}.", jsonElement);
		try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
			ActionResult<Wo> result = new ActionResult<>();
			Wo wo = new Wo();
			Wi wi = this.convertToWrapIn(jsonElement, Wi.class);
			Business business = new Business(emc);
R
roo00 已提交
37
			if (!business.controllable(effectivePerson)) {
caixiangyi's avatar
caixiangyi 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
				throw new ExceptionQueryAccessDenied(effectivePerson.getName(), wi.getName(), wi.getId());
			}
			Query query = this.create(business, wi);
			wo.setId(query.getId());
			result.setData(wo);
			return result;
		}
	}

	private Query create(Business business, Wi wi) throws Exception {
		List<JpaObject> persistObjects = new ArrayList<>();
		Query query = business.entityManagerContainer().find(wi.getId(), Query.class);
		if (null != query) {
			throw new ExceptionQueryExist(wi.getId());
		}
		query = WrapQuery.inCopier.copy(wi);
		query.setName(this.idleQueryName(business, query.getName(), query.getId()));
		query.setAlias(this.idleQueryAlias(business, query.getAlias(), query.getId()));
		persistObjects.add(query);
		for (WrapView _o : wi.getViewList()) {
			View obj = business.entityManagerContainer().find(_o.getId(), View.class);
			if (null != obj) {
				throw new ExceptionEntityExistForCreate(_o.getId(), View.class);
			}
			obj = WrapView.inCopier.copy(_o);
			obj.setQuery(query.getId());
			persistObjects.add(obj);
		}
		for (WrapStat _o : wi.getStatList()) {
			Stat obj = business.entityManagerContainer().find(_o.getId(), Stat.class);
			if (null != obj) {
				throw new ExceptionEntityExistForCreate(_o.getId(), Stat.class);
			}
			obj = WrapStat.inCopier.copy(_o);
			obj.setQuery(query.getId());
			persistObjects.add(obj);
		}
R
update  
roo00 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
		for (WrapTable _o : wi.getTableList()) {
			Table obj = business.entityManagerContainer().find(_o.getId(), Table.class);
			if (null != obj) {
				throw new ExceptionEntityExistForCreate(_o.getId(), Table.class);
			}
			obj = WrapTable.inCopier.copy(_o);
			obj.setQuery(query.getId());
			persistObjects.add(obj);
		}
		for (WrapStatement _o : wi.getStatementList()) {
			Statement obj = business.entityManagerContainer().find(_o.getId(), Statement.class);
			if (null != obj) {
				throw new ExceptionEntityExistForCreate(_o.getId(), Statement.class);
			}
			obj = WrapStatement.inCopier.copy(_o);
			obj.setQuery(query.getId());
			persistObjects.add(obj);
		}
caixiangyi's avatar
caixiangyi 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105
		for (WrapReveal _o : wi.getRevealList()) {
			Reveal obj = business.entityManagerContainer().find(_o.getId(), Reveal.class);
			if (null != obj) {
				throw new ExceptionEntityExistForCreate(_o.getId(), Reveal.class);
			}
			obj = WrapReveal.inCopier.copy(_o);
			obj.setQuery(query.getId());
			persistObjects.add(obj);
		}
		business.entityManagerContainer().beginTransaction(Query.class);
		business.entityManagerContainer().beginTransaction(View.class);
		business.entityManagerContainer().beginTransaction(Stat.class);
		business.entityManagerContainer().beginTransaction(Reveal.class);
R
update  
roo00 已提交
106 107
		business.entityManagerContainer().beginTransaction(Table.class);
		business.entityManagerContainer().beginTransaction(Statement.class);
caixiangyi's avatar
caixiangyi 已提交
108 109 110 111
		for (JpaObject o : persistObjects) {
			business.entityManagerContainer().persist(o);
		}
		business.entityManagerContainer().commit();
R
update  
roo00 已提交
112
		if(!wi.getTableList().isEmpty()){
O
o2sword 已提交
113 114
			CacheManager.notify(Table.class);
			CacheManager.notify(Statement.class);
R
update  
roo00 已提交
115
		}else if(!wi.getStatementList().isEmpty()){
O
o2sword 已提交
116
			CacheManager.notify(Statement.class);
R
update  
roo00 已提交
117
		}
caixiangyi's avatar
caixiangyi 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131
		return query;
	}

	public static class Wi extends WrapQuery {

		private static final long serialVersionUID = -4612391443319365035L;

	}

	public static class Wo extends WoId {

	}

}