- 1.7.6 초기 코드
[Tadpole.git] / com.hangum.tadpole.commons.sql / src / com / hangum / tadpole / engine / query / sql / TadpoleSystem_UserDBResource.java
blobbcec73ec96cba05202c24f7395c2262d5551f855
1 /*******************************************************************************
2 * Copyright (c) 2013 hangum.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser Public License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
7 *
8 * Contributors:
9 * hangum - initial API and implementation
10 ******************************************************************************/
11 package com.hangum.tadpole.engine.query.sql;
13 import java.sql.SQLException;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
19 import org.apache.log4j.Logger;
21 import com.hangum.tadpole.commons.exception.TadpoleRuntimeException;
22 import com.hangum.tadpole.commons.exception.TadpoleSQLManagerException;
23 import com.hangum.tadpole.commons.libs.core.define.PublicTadpoleDefine;
24 import com.hangum.tadpole.commons.libs.core.define.PublicTadpoleDefine.RESOURCE_TYPE;
25 import com.hangum.tadpole.engine.Messages;
26 import com.hangum.tadpole.engine.initialize.TadpoleEngineUserDB;
27 import com.hangum.tadpole.engine.manager.TadpoleSQLManager;
28 import com.hangum.tadpole.engine.query.dao.ResourceManagerDAO;
29 import com.hangum.tadpole.engine.query.dao.system.UserDBDAO;
30 import com.hangum.tadpole.engine.query.dao.system.UserDBResourceDAO;
31 import com.hangum.tadpole.engine.query.dao.system.UserDBResourceDataDAO;
32 import com.hangum.tadpole.engine.restful.RESTFulNotFoundURLException;
33 import com.hangum.tadpole.session.manager.SessionManager;
34 import com.ibatis.sqlmap.client.SqlMapClient;
36 /**
37 * user_db_erd 관련 코드
39 * @author hangum
42 public class TadpoleSystem_UserDBResource {
43 private static final Logger logger = Logger.getLogger(TadpoleSystem_UserDBResource.class);
45 /**
46 * Returns restful api list
48 * @param intUserSeq
49 * @return
50 * @throws SQLException
51 * @throws TadpoleSQLManagerException, SQLException
53 public static List<ResourceManagerDAO> getRESTFulAPIList() throws TadpoleSQLManagerException, SQLException {
54 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
55 return (List<ResourceManagerDAO>)sqlClient.queryForList("getRESTFulAPIList", SessionManager.getUserSeq()); //$NON-NLS-1$
58 /**
59 * find restful
61 * @param mapCredential
62 * @return
63 * @throws RESTFulNotFoundURLException
64 * @throws SQLException
65 * @throws TadpoleSQLManagerException
66 * @throws TadpoleSQLManagerException, SQLException
68 public static UserDBResourceDAO findRESTURL(int intUserSEQ, String strUserDomainURL) throws RESTFulNotFoundURLException, SQLException, TadpoleSQLManagerException {
69 Map<String, Object> mapCredential = new HashMap<String, Object>();
70 mapCredential.put("user_seq", intUserSEQ); //$NON-NLS-1$
71 mapCredential.put("restapi_uri", strUserDomainURL); //$NON-NLS-1$
73 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
74 UserDBResourceDAO userResource = (UserDBResourceDAO)sqlClient.queryForObject("findRESTURL", mapCredential); //$NON-NLS-1$
76 if(userResource == null) {
77 throw new RESTFulNotFoundURLException("Not found your request url. Check your URL.");
80 return userResource;
83 /**
84 * find api
85 * @param strAPIKey
86 * @return
87 * @throws TadpoleSQLManagerException, SQLException
89 public static UserDBResourceDAO findAPIKey(String strAPIKey) throws TadpoleSQLManagerException, SQLException {
90 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
91 UserDBResourceDAO userResource = (UserDBResourceDAO)sqlClient.queryForObject("findAPIKey", strAPIKey); //$NON-NLS-1$
93 return userResource;
96 /**
97 * 저장
99 * @param userDB
100 * @param userDBResource
101 * @param contents
103 * @throws TadpoleSQLManagerException, SQLException
105 public static UserDBResourceDAO saveResource(UserDBDAO userDB, UserDBResourceDAO userDBResource, String contents) throws TadpoleSQLManagerException, SQLException {
106 // 기존에 등록 되어 있는지 검사한다
107 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
108 UserDBResourceDAO retUserDBResource = (UserDBResourceDAO)sqlClient.insert("userDbResourceInsert", userDBResource); //$NON-NLS-1$
110 // content data를 저장합니다.
111 insertResourceData(retUserDBResource, contents);
113 return retUserDBResource;
117 * update resource_title & shared_type
119 * @param dbResource
120 * @param content
121 * @throws TadpoleSQLManagerException, SQLException
123 public static void updateResourceHeader(ResourceManagerDAO userDBResource) throws TadpoleSQLManagerException, SQLException {
124 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
125 sqlClient.update("userDbResourceUpdate", userDBResource); //$NON-NLS-1$
128 // /**
129 // * update resource_title & description
130 // *
131 // * @param dbResource
132 // * @param content
133 // * @throws TadpoleSQLManagerException, SQLException
134 // */
135 // public static void userDbResourceHeadUpdate(ResourceManagerDAO userDBResource) throws TadpoleSQLManagerException, SQLException {
136 // SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleSystemInitializer.getUserDB());
137 // sqlClient.update("userDbResourceHeadUpdate", userDBResource); //$NON-NLS-1$
138 // }
141 * updateResourceAuto
143 * @param dbResource
144 * @param content
145 * @throws TadpoleSQLManagerException, SQLException
147 public static void updateResourceAuto(UserDBResourceDAO userDBResource, String contents) throws TadpoleSQLManagerException, SQLException {
148 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
149 sqlClient.delete("userDbResourceDataAutoDelete", userDBResource.getResource_seq()); //$NON-NLS-1$
151 insertResourceData(userDBResource, contents);
155 * update
157 * @param dbResource
158 * @param content
159 * @throws TadpoleSQLManagerException, SQLException
161 public static void updateResource(UserDBResourceDAO userDBResource, String contents) throws TadpoleSQLManagerException, SQLException {
162 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
163 sqlClient.update("userDbResourceDataDelete", userDBResource.getResource_seq()); //$NON-NLS-1$
165 insertResourceData(userDBResource, contents);
169 * auto resource data
171 * @param userDB
172 * @param dBResource
173 * @param dBResource2
174 * @param newContents
176 public static UserDBResourceDAO updateAutoResourceDate(UserDBDAO userDB, UserDBResourceDAO dBResourceAuto, UserDBResourceDAO dBResource, String newContents) throws TadpoleSQLManagerException, SQLException {
177 // auto save 도 시작.
178 if(dBResource == null && dBResourceAuto == null) {
179 if(logger.isDebugEnabled()) logger.debug("=[0]===[updateAutoResourceDate][dBResource is null][dBResourceAuto is null]============");
180 UserDBResourceDAO defDao = new UserDBResourceDAO();
181 defDao.setDb_seq(userDB.getSeq());
182 defDao.setName(PublicTadpoleDefine.DEFAUL_RESOURCE_NAME);
183 defDao.setResource_types(RESOURCE_TYPE.AUTO_SQL.name());
184 defDao.setShared_type(PublicTadpoleDefine.SHARED_TYPE.PRIVATE.name());
185 defDao.setRestapi_yesno(PublicTadpoleDefine.YES_NO.NO.name());
186 defDao.setUser_seq(SessionManager.getUserSeq());
188 // 기존에 등록 되어 있는지 검사한다
189 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
190 UserDBResourceDAO retUserDBResource = (UserDBResourceDAO)sqlClient.insert("userDbResourceInsert", defDao); //$NON-NLS-1$
191 insertResourceData(retUserDBResource, newContents);
193 return retUserDBResource;
194 // auto save 가 한번이라도 있었음.
195 } else if(dBResource == null && dBResourceAuto != null) {
196 if(logger.isDebugEnabled()) logger.debug("=[1]===[updateAutoResourceDate][dBResource is null][dBResourceAuto is not null]============");
197 // 기존에 등록 되어 있는 것을 삭제하고 저장한다.
198 updateResourceAuto(dBResourceAuto, newContents);
200 return dBResourceAuto;
201 } else {
202 if(logger.isDebugEnabled()) logger.debug("=[2]===[updateAutoResourceDate][dBResource is not null]============");
203 // 기존에 등록 되어 있는지 검사한다
204 updateResource(dBResource, newContents);
206 return dBResource;
211 * resource data
213 * @param userDBResource
214 * @param contents
215 * @throws TadpoleSQLManagerException, SQLException
217 private static void insertResourceData(UserDBResourceDAO userDBResource, String contents) throws TadpoleSQLManagerException, SQLException {
218 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
220 // content data를 저장합니다.
221 UserDBResourceDataDAO dataDao = new UserDBResourceDataDAO();
222 dataDao.setUser_db_resource_seq(userDBResource.getResource_seq());
223 dataDao.setGroup_seq(System.currentTimeMillis());
224 dataDao.setUser_seq(SessionManager.getUserSeq());
226 dataDao.setDatas(contents);
227 sqlClient.insert("userDbResourceDataInsert", dataDao); //$NON-NLS-1$
231 * 해당 db의 모든 erd리스트를 가져온다. (필터되는것은 가져오지 않는다)
233 * @param userDB
234 * @return
235 * @throws TadpoleSQLManagerException, SQLException
237 public static List<UserDBResourceDAO> userDbResourceTree(UserDBDAO userDB) throws TadpoleSQLManagerException, SQLException {
238 List<UserDBResourceDAO> listRealResource = new ArrayList<UserDBResourceDAO>();
240 Map<String, Object> queryMap = new HashMap<String, Object>();
241 queryMap.put("seq", userDB.getSeq()); //$NON-NLS-1$
243 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
244 List<UserDBResourceDAO> listUserDBResources = (List<UserDBResourceDAO>)sqlClient.queryForList("userDbResourceTree", queryMap); //$NON-NLS-1$
245 for (UserDBResourceDAO userDBResourceDAO : listUserDBResources) {
246 if(PublicTadpoleDefine.SHARED_TYPE.PUBLIC.toString().equals(userDBResourceDAO.getShared_type())) {
247 userDBResourceDAO.setParent(userDB);
248 listRealResource.add(userDBResourceDAO);
249 } else {
251 // 리소스 중에서 개인 리소스만 넣도록 합니다.
252 if(SessionManager.getUserSeq() == userDBResourceDAO.getUser_seq()) {
253 userDBResourceDAO.setParent(userDB);
254 listRealResource.add(userDBResourceDAO);
259 return listRealResource;
263 * 리소스 목록을 조회한다.
264 * @param <ResourceManagerDAO>
265 * @param <ResourceManagerDAO>
267 * @param userDB
268 * @return
269 * @throws TadpoleSQLManagerException, SQLException
271 public static List<ResourceManagerDAO> userDbResource(UserDBDAO userDB) throws TadpoleSQLManagerException, SQLException {
272 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
273 return (List<ResourceManagerDAO>)sqlClient.queryForList("userDbResourceManager", userDB); //$NON-NLS-1$
277 * 이름 또는 api url이 중복되었는지 검사
279 * @param userDB
280 * @param retResourceDao
281 * @return
282 * @throws TadpoleSQLManagerException, SQLException
284 public static void userDBResourceDuplication(UserDBDAO userDB, UserDBResourceDAO dbResourceDAO) throws TadpoleSQLManagerException, SQLException {
285 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
286 if(!sqlClient.queryForList("userDBResourceDuplication", dbResourceDAO).isEmpty()) { //$NON-NLS-1$
287 throw new TadpoleRuntimeException(Messages.get().TadpoleSystem_UserDBResource_6);
290 if(!dbResourceDAO.getRestapi_uri().equals("")) {
291 if(!sqlClient.queryForList("userDBResourceAPIDuplication", dbResourceDAO).isEmpty()) { //$NON-NLS-1$
292 throw new TadpoleRuntimeException(Messages.get().TadpoleSystem_UserDBResource_8);
298 * 리소스 업데이트시 중복 오류 검사.
300 * @param userDB
301 * @param dao
303 public static void userDBResourceDupUpdate(UserDBDAO userDB, ResourceManagerDAO dao) throws TadpoleSQLManagerException, SQLException {
304 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
305 if(!sqlClient.queryForList("userDBResourceDuplicationUpdate", dao).isEmpty()) { //$NON-NLS-1$
306 throw new TadpoleRuntimeException(Messages.get().TadpoleSystem_UserDBResource_6);
309 if(!dao.getRestapi_uri().equals("")) {
310 if(!sqlClient.queryForList("userDBResourceAPIDuplicationUpdate", dao).isEmpty()) { //$NON-NLS-1$
311 throw new TadpoleRuntimeException(Messages.get().TadpoleSystem_UserDBResource_8);
317 * default db resource data
319 * @param userDB
320 * @param userSeq
321 * @return
322 * @throws TadpoleSQLManagerException
323 * @throws SQLException
325 public static UserDBResourceDAO getDefaultDBResourceData(UserDBDAO userDB) throws TadpoleSQLManagerException, SQLException {
326 Map<String, Object> queryMap = new HashMap<String, Object>();
327 queryMap.put("db_seq", userDB.getSeq()); //$NON-NLS-1$
328 queryMap.put("user_seq", SessionManager.getUserSeq()); //$NON-NLS-1$
330 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
331 UserDBResourceDAO resourcesDao = (UserDBResourceDAO)sqlClient.queryForObject("userDBResourceDefault", queryMap); //$NON-NLS-1$
332 if(resourcesDao == null) return null;
334 String strResource = getResourceData(resourcesDao);
335 resourcesDao.setDataString(strResource);
337 return resourcesDao;
342 * user_db_resource 삭제.
343 * user_db_resource_data 삭제.
345 * @param userDBErd
346 * @throws TadpoleSQLManagerException, SQLException
348 public static void delete(UserDBResourceDAO userDBErd) throws TadpoleSQLManagerException, SQLException {
349 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
350 sqlClient.update("userDBResourceDelete", userDBErd); //$NON-NLS-1$
354 * userdb의 resource data를 얻습니다.
356 * @param userDBResource
357 * @throws TadpoleSQLManagerException, SQLException
359 public static String getResourceData(UserDBResourceDAO userDBResource) throws TadpoleSQLManagerException, SQLException {
360 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
361 List<UserDBResourceDataDAO> datas = (List<UserDBResourceDataDAO>)sqlClient.queryForList("userDBResourceData", userDBResource); //$NON-NLS-1$
363 StringBuffer retData = new StringBuffer();
364 for (UserDBResourceDataDAO userDBResourceDataDAO : datas) {
365 retData.append(userDBResourceDataDAO.getDatas());
368 return retData.toString();
372 * resource data history
374 * @param userDBResource
375 * @return
376 * @throws TadpoleSQLManagerException
377 * @throws SQLException
379 public static List<UserDBResourceDataDAO> getResouceDataHistory(ResourceManagerDAO userDBResource) throws TadpoleSQLManagerException, SQLException {
380 SqlMapClient sqlClient = TadpoleSQLManager.getInstance(TadpoleEngineUserDB.getUserDB());
381 List<UserDBResourceDataDAO> datas = (List<UserDBResourceDataDAO>)sqlClient.queryForList("getResouceDataHistory", userDBResource.getResource_seq()); //$NON-NLS-1$
382 return datas;