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
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
;
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
;
42 public class TadpoleSystem_UserDBResource
{
43 private static final Logger logger
= Logger
.getLogger(TadpoleSystem_UserDBResource
.class);
46 * Returns restful api list
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$
61 * @param mapCredential
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.");
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$
100 * @param userDBResource
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
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$
129 // * update resource_title & description
131 // * @param dbResource
133 // * @throws TadpoleSQLManagerException, SQLException
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$
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
);
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
);
176 public static UserDBResourceDAO
updateAutoResourceDate(UserDBDAO userDB
, UserDBResourceDAO dBResourceAuto
, UserDBResourceDAO dBResource
, String newContents
) throws TadpoleSQLManagerException
, SQLException
{
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
;
202 if(logger
.isDebugEnabled()) logger
.debug("=[2]===[updateAutoResourceDate][dBResource is not null]============");
203 // 기존에 등록 되어 있는지 검사한다
204 updateResource(dBResource
, newContents
);
213 * @param userDBResource
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리스트를 가져온다. (필터되는것은 가져오지 않는다)
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
);
251 // 리소스 중에서 개인 리소스만 넣도록 합니다.
252 if(SessionManager
.getUserSeq() == userDBResourceDAO
.getUser_seq()) {
253 userDBResourceDAO
.setParent(userDB
);
254 listRealResource
.add(userDBResourceDAO
);
259 return listRealResource
;
264 * @param <ResourceManagerDAO>
265 * @param <ResourceManagerDAO>
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이 중복되었는지 검사
280 * @param retResourceDao
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 * 리소스 업데이트시 중복 오류 검사.
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
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
);
342 * user_db_resource 삭제.
343 * user_db_resource_data 삭제.
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
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$