7bee36e65cb44a7224610c74d67ebf3ceb0585f2
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 package org.openecomp.portalsdk.analytics.model.base;
21
22 import java.util.*;
23
24 import org.openecomp.portalsdk.analytics.error.RaptorException;
25 import org.openecomp.portalsdk.analytics.model.*;
26 import org.openecomp.portalsdk.analytics.model.runtime.*;
27 import org.openecomp.portalsdk.analytics.system.*;
28 import org.openecomp.portalsdk.analytics.util.*;
29
30 public class IdNameLookup extends IdNameSql {
31         private String dbTableName = null;
32
33         private String dbIdField = null;
34
35         private String dbNameField = null;
36
37         private String dbSortByField = null;
38
39         private String searchString = "";
40
41         public IdNameLookup(int pageNo, String dbTableName, String dbIdField, String dbNameField) {
42                 this(dbTableName, dbIdField, dbNameField);
43
44                 this.pageNo = pageNo;
45         } // IdNameLookup
46
47         public IdNameLookup(String dbTableName, String dbIdField, String dbNameField) {
48                 this(dbTableName, dbIdField, dbNameField, null, "", false);
49         } // IdNameLookup
50
51 /*    public IdNameLookup(String dbTableName, String dbIdField, String dbNameField,
52             String dbSortByField) {
53         super();
54
55         setDbTableName(dbTableName);
56         setDbIdField(dbIdField);
57         setDbNameField(dbNameField);
58         setDbSortByField(dbSortByField);
59         updateParentSQL();
60     } // IdNameLookup
61
62     public IdNameLookup(String dbTableName, String dbIdField, String dbNameField,
63                         String dbSortByField, String defaultSQL) {
64                 super();
65
66                 setDbTableName(dbTableName);
67                 setDbIdField(dbIdField);
68                 setDbNameField(dbNameField);
69                 setDbSortByField(dbSortByField);
70         setDefaultSQL(defaultSQL);        
71                 updateParentSQL();
72         } // IdNameLookup
73 */
74     public IdNameLookup(String dbTableName, String dbIdField, String dbNameField,
75             String dbSortByField, boolean textField) {
76         super();
77         setDbTableName(dbTableName);
78         setDbIdField(dbIdField);
79         setDbNameField(dbNameField);
80         setDbSortByField(dbSortByField);
81         if(!textField)
82                 updateParentSQL();
83     } // IdNameLookup
84
85     public IdNameLookup(String dbTableName, String dbIdField, String dbNameField,
86                         String dbSortByField, String defaultSQL, boolean textField) {
87                 super();
88
89                 setDbTableName(dbTableName);
90                 setDbIdField(dbIdField);
91                 setDbNameField(dbNameField);
92                 setDbSortByField(dbSortByField);
93         setDefaultSQL(defaultSQL);  
94         if(!textField)
95                 updateParentSQL();
96         } // IdNameLookup
97
98     public String getDbTableName() {
99                 return dbTableName;
100         }
101
102         public String getDbIdField() {
103                 return dbIdField;
104         }
105
106         public String getDbNameField() {
107                 return dbNameField;
108         }
109
110         public String getDbSortByField() {
111                 return dbSortByField;
112         }
113
114         public void setDbTableName(String dbTableName) {
115                 this.dbTableName = dbTableName;
116         }
117
118         public void setDbIdField(String dbIdField) {
119                 this.dbIdField = dbIdField;
120         }
121
122         public void setDbNameField(String dbNameField) {
123                 this.dbNameField = dbNameField;
124         }
125
126
127         public void setDbSortByField(String dbSortByField) {
128                 this.dbSortByField = dbSortByField;
129         }
130
131         private void updateParentSQL() {
132                 String sql_start = "SELECT DISTINCT " + dbIdField + " id, " + dbNameField + " name";
133                 String sql_end = " FROM " + dbTableName + " WHERE " + dbIdField + " IS NOT NULL";
134                 if (searchString.length() > 0)
135                         sql_end += " AND UPPER(" + dbNameField + ") LIKE UPPER('" + searchString + "')";
136
137                 String sql_middle = "";
138                 if (dbSortByField != null && (!dbSortByField.equals(dbNameField)) && (!dbSortByField.trim().startsWith("TO_DATE")))
139                         sql_middle = ", "
140                                         + ((dbSortByField.indexOf(' ') > 0) ? dbSortByField.substring(0,
141                                                         dbSortByField.indexOf(' ')) : dbSortByField) + " sort";
142
143                 setSqlNoOrderBy(sql_start + sql_middle + sql_end);
144 //        System.out.println("SQL Start " + sql_start);
145 //        System.out.println("SQL Middle " + sql_middle);
146 //        System.out.println("SQL End " + sql_end);
147 //        System.out.println("DbSortByField " + dbSortByField);
148         
149                 setSql(sql_start + sql_middle + sql_end + " ORDER BY " + nvl(dbSortByField, "2"));
150         } // updateParentSQL
151
152         public boolean canUseSearchString() {
153                 return true;
154         }
155
156         public String getBaseSQL() {
157                 return "SELECT " + dbIdField + " FROM " + dbTableName;
158         } // getBaseSQL
159
160         public String getBaseWholeSQL() {
161                 return "SELECT " + dbIdField + " FROM " + dbTableName;
162         } // getBaseSQL
163         
164         /*
165         public void loadData(int pageNo) throws RaptorException {
166                 loadData(pageNo, "");
167         } // loadData
168
169         public void loadData(String pageNo) throws RaptorException {
170                 loadData(pageNo, "");
171         } // loadData
172 */
173         
174         public void loadData(String pageNo, String searchString, String dbInfo) throws RaptorException {
175                 int iPageNo = 0;
176
177                 if (pageNo != null)
178                         try {
179                                 iPageNo = Integer.parseInt(pageNo);
180                         } catch (NumberFormatException e) {
181                         }
182
183                 loadData(iPageNo, searchString, dbInfo);
184         } // loadData
185
186         private void loadData(int pageNo, String searchString, String dbInfo) throws RaptorException {
187                 boolean dataAlreadyLoaded = (this.pageNo == pageNo)
188                                 && (this.searchString.equals(searchString));
189
190                 if (dataAlreadyLoaded)
191                         return;
192
193                 if (!this.searchString.equals(searchString)) {
194                         dataSize = -1;
195                         pageNo = 0;
196                 } // if
197
198                 this.pageNo = pageNo;
199                 this.searchString = searchString;
200                 updateParentSQL();
201                 performLoadData(searchString,dbInfo);
202         } // loadData
203
204 } // IdNameLookup