938b9d377f0a7847fff85f629da19673ac79723a
[so.git] /
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.camunda.bpmn.plugin.urnmap.resources;\r
22 /***\r
23 import java.beans.Statement;\r
24 import java.sql.DriverManager;\r
25 import java.sql.ResultSet;\r
26 import java.sql.SQLException;\r
27 */\r
28 import java.sql.Connection;\r
29 import java.sql.PreparedStatement;\r
30 import java.sql.ResultSet;\r
31 import java.sql.SQLException;\r
32 import java.util.ArrayList;\r
33 import java.util.List;\r
34 import java.util.StringTokenizer;\r
35 \r
36 import javax.naming.Context;\r
37 import javax.naming.InitialContext;\r
38 import javax.sql.DataSource;\r
39 import javax.ws.rs.GET;\r
40 import javax.ws.rs.POST;\r
41 import javax.ws.rs.PUT;\r
42 \r
43 import org.camunda.bpm.cockpit.db.QueryParameters;\r
44 import org.camunda.bpm.cockpit.plugin.resource.AbstractCockpitPluginResource;\r
45 \r
46 \r
47 //import org.camunda.bpm.cockpit.plugin.resource.AbstractPluginResource;\r
48 import org.openecomp.camunda.bpmn.plugin.urnmap.db.URNData;\r
49 import org.openecomp.mso.logger.MsoLogger;\r
50 \r
51 \r
52 //public class ProcessInstanceResource extends AbstractPluginResource {\r
53 public class URNResource extends AbstractCockpitPluginResource{\r
54   public URNResource(String engineName) {\r
55     super(engineName);\r
56   }\r
57   \r
58   private Connection conn;\r
59   private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);\r
60   @GET\r
61   public List<URNData> getUrnDataMap() \r
62   {  \r
63           List<URNData> list = new ArrayList();\r
64           PreparedStatement psData = null;\r
65           try {\r
66                         \r
67                     conn = getDBConnection();\r
68                         psData = conn\r
69                                                 .prepareStatement("select * from MSO_URN_MAPPING order by NAME_");\r
70                             \r
71                             ResultSet r = psData.executeQuery();\r
72                             \r
73                                 while(r.next()) \r
74                                 {\r
75                                         URNData d = new URNData();\r
76                                         d.setURNName(r.getString("NAME_"));\r
77                                         d.setURNValue(r.getString("VALUE_"));\r
78                                         d.setVer_( r.getString("REV_"));\r
79                                         \r
80                                         list.add(d);                                    \r
81                                 }\r
82                                 \r
83                 } catch (Exception e) \r
84                 {\r
85                         msoLogger.debug("Exception in getUrnDataMap ", e);                      \r
86                 } finally {\r
87                         if(psData != null){\r
88                                 try {\r
89                                         psData.close();\r
90                                         conn.close();\r
91                                 } catch (SQLException e) {\r
92                                         msoLogger.debug("Exception while closing the PreparedStatement: ", e);\r
93                                 }\r
94                         }\r
95                         if(conn != null){\r
96                                 try {\r
97                                         conn.close();\r
98                                 } catch (SQLException e) {\r
99                                         msoLogger.debug("Exception while closing the connection: ", e);\r
100                                 }\r
101                         }\r
102                 }\r
103      \r
104     for(URNData d: list)\r
105     {\r
106         msoLogger.debug(  d.getURNName() + "            "  + d.getURNValue());\r
107         //msoLogger.debug("Started Executing " + getTaskName());\r
108         msoLogger.debug("Started Executing " + d.getURNName() + " " + d.getURNValue());\r
109     }\r
110    \r
111     return list;\r
112   }\r
113   \r
114   public List<URNData> getUrnDataMapOLD() \r
115   {  \r
116           \r
117     List<URNData> list = getQueryService()\r
118             .executeQuery("cockpit.urnMap.retrieveUrnKeyValuePair", new QueryParameters<URNData>());\r
119     \r
120     msoLogger.debug("urnmap-plugin project - Results Retrieved: ");\r
121     msoLogger.debug("URNName: " + "             " + "URNValue: " );\r
122     \r
123     for(URNData d: list)\r
124     {\r
125         msoLogger.debug( d.getURNName() + "             "  + d.getURNValue());\r
126     }\r
127    \r
128     return list;\r
129   }\r
130   \r
131   public Connection getDBConnection()\r
132   {\r
133           try {\r
134                         \r
135                         if(conn == null)\r
136                         {\r
137                                 Context ctx = new InitialContext();\r
138                             DataSource ds = (DataSource)ctx.lookup("java:jboss/datasources/ProcessEngine");//jboss\r
139                         conn =                  ds.getConnection();\r
140          \r
141                         }                        \r
142                         \r
143                 } catch (Exception e) \r
144                 {\r
145                         msoLogger.debug("Exception in DBConnection ", e);\r
146                 }\r
147           \r
148           return conn;\r
149   }\r
150   \r
151   @PUT\r
152   public void insertNewRow(String temp) \r
153    {  \r
154          msoLogger.debug("AddNewRow: XXXXXXXXXXXXXXXXX ---> " + temp);\r
155          msoLogger.debug("AddNewRow: EngineName  ---> " + engineName);\r
156          \r
157          StringTokenizer st = new StringTokenizer(temp, "|");\r
158          String key_ = "";\r
159          String value_ = "";\r
160          \r
161          while(st.hasMoreTokens()) { \r
162                   key_ = st.nextToken(); \r
163                   value_ = st.nextToken(); \r
164                  msoLogger.debug(key_ + "\t" + value_); \r
165                  } \r
166                          \r
167        msoLogger.debug("AddNewRow: XXXXXXXXXXXXXXXXX ---> key: " + key_ + " , Value: " + value_);\r
168           final URNData nRow = new URNData();\r
169           nRow.setVer_("1");\r
170           PreparedStatement psData = null;\r
171           \r
172                 msoLogger.debug("----------- START ----------------------");\r
173                 try {\r
174                         \r
175                     conn = getDBConnection();\r
176                         psData = conn\r
177                                                 .prepareStatement("Insert into MSO_URN_MAPPING values ('" + key_ + "', '" + value_  + "', '1')");\r
178                             \r
179                             psData.executeUpdate();              \r
180                         \r
181                 } catch (Exception e) \r
182                 {\r
183                         msoLogger.debug("Exception in insertNewRow ", e);\r
184                 }finally {\r
185                         if(psData != null){\r
186                                 try {\r
187                                         psData.close();\r
188                                         conn.close();\r
189                                 } catch (SQLException e) {\r
190                                         msoLogger.debug("Exception while closing the PreparedStatement: ", e);\r
191                                 }\r
192                         }\r
193                         if(conn != null){\r
194                                 try {\r
195                                         conn.close();\r
196                                 } catch (SQLException e) {\r
197                                         msoLogger.debug("Exception while closing the connection: ", e);\r
198                                 }\r
199                         }\r
200                 }\r
201         // getQueryService().executeQuery("cockpit.urnMap.insertNewRow", nRow, URNData.class);\r
202    }\r
203   \r
204   @POST\r
205   public void getPersistData(URNData d) {  \r
206           \r
207                 //getQueryService().executeQuery("cockpit.urnMap.persistURNData", d, URNData.class);\r
208             PreparedStatement psData = null;\r
209           try {\r
210                         \r
211                     conn = getDBConnection();\r
212                         psData = conn\r
213                                                 .prepareStatement("UPDATE MSO_URN_MAPPING set VALUE_ ='"+ d.getURNValue() + "' WHERE NAME_='" + d.getURNName() + "'");\r
214                             \r
215                             psData.executeUpdate();\r
216                            \r
217                 } catch (Exception e) \r
218                 {\r
219                         msoLogger.debug("Exception in PersistData ", e);\r
220                 }finally {\r
221                         if(psData != null){\r
222                                 try {\r
223                                         psData.close();\r
224                                         conn.close();\r
225                                 } catch (SQLException e) {\r
226                                         msoLogger.debug("Exception while closing the PreparedStatement: ", e);\r
227                                 }\r
228                         }\r
229                         if(conn != null){\r
230                                 try {\r
231                                         conn.close();\r
232                                 } catch (SQLException e) {\r
233                                         msoLogger.debug("Exception while closing the connection: ", e);\r
234                                 }\r
235                         }\r
236                 }\r
237          \r
238         }\r
239 }\r