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