summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-analytics/src/main/java/org/openecomp/portalsdk/analytics/model/base/IdNameLookup.java
blob: 73f65c89aab5092c8e3be5fc56336baf71880f80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*-
 * ================================================================================
 * eCOMP Portal SDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ================================================================================
 */
package org.openecomp.portalsdk.analytics.model.base;

import java.util.*;

import org.openecomp.portalsdk.analytics.error.RaptorException;
import org.openecomp.portalsdk.analytics.model.*;
import org.openecomp.portalsdk.analytics.model.runtime.*;
import org.openecomp.portalsdk.analytics.system.*;
import org.openecomp.portalsdk.analytics.util.*;

public class IdNameLookup extends IdNameSql {
	private String dbTableName = null;

	private String dbIdField = null;

	private String dbNameField = null;

	private String dbSortByField = null;

	private String searchString = "";

	public IdNameLookup(int pageNo, String dbTableName, String dbIdField, String dbNameField) {
		this(dbTableName, dbIdField, dbNameField);

		this.pageNo = pageNo;
	} // IdNameLookup

	public IdNameLookup(String dbTableName, String dbIdField, String dbNameField) {
		this(dbTableName, dbIdField, dbNameField, null, "", false);
	} // IdNameLookup

/*    public IdNameLookup(String dbTableName, String dbIdField, String dbNameField,
            String dbSortByField) {
        super();

        setDbTableName(dbTableName);
        setDbIdField(dbIdField);
        setDbNameField(dbNameField);
        setDbSortByField(dbSortByField);
        updateParentSQL();
    } // IdNameLookup

    public IdNameLookup(String dbTableName, String dbIdField, String dbNameField,
			String dbSortByField, String defaultSQL) {
		super();

		setDbTableName(dbTableName);
		setDbIdField(dbIdField);
		setDbNameField(dbNameField);
		setDbSortByField(dbSortByField);
        setDefaultSQL(defaultSQL);        
		updateParentSQL();
	} // IdNameLookup
*/
    public IdNameLookup(String dbTableName, String dbIdField, String dbNameField,
            String dbSortByField, boolean textField) {
        super();
        setDbTableName(dbTableName);
        setDbIdField(dbIdField);
        setDbNameField(dbNameField);
        setDbSortByField(dbSortByField);
        if(!textField)
        	updateParentSQL();
    } // IdNameLookup

    public IdNameLookup(String dbTableName, String dbIdField, String dbNameField,
			String dbSortByField, String defaultSQL, boolean textField) {
		super();

		setDbTableName(dbTableName);
		setDbIdField(dbIdField);
		setDbNameField(dbNameField);
		setDbSortByField(dbSortByField);
        setDefaultSQL(defaultSQL);  
        if(!textField)
        	updateParentSQL();
	} // IdNameLookup

    public String getDbTableName() {
		return dbTableName;
	}

	public String getDbIdField() {
		return dbIdField;
	}

	public String getDbNameField() {
		return dbNameField;
	}

	public String getDbSortByField() {
		return dbSortByField;
	}

	public void setDbTableName(String dbTableName) {
		this.dbTableName = dbTableName;
	}

	public void setDbIdField(String dbIdField) {
		this.dbIdField = dbIdField;
	}

	public void setDbNameField(String dbNameField) {
		this.dbNameField = dbNameField;
	}


	public void setDbSortByField(String dbSortByField) {
		this.dbSortByField = dbSortByField;
	}

	private void updateParentSQL() {
		String sql_start = "SELECT DISTINCT " + dbIdField + " id, " + dbNameField + " name";
		String sql_end = " FROM " + dbTableName + " WHERE " + dbIdField + " IS NOT NULL";
		if (searchString.length() > 0)
			sql_end += " AND UPPER(" + dbNameField + ") LIKE UPPER('" + searchString + "')";

		String sql_middle = "";
		if (dbSortByField != null && (!dbSortByField.equals(dbNameField)) && (!dbSortByField.trim().startsWith("TO_DATE")))
			sql_middle = ", "
					+ ((dbSortByField.indexOf(' ') > 0) ? dbSortByField.substring(0,
							dbSortByField.indexOf(' ')) : dbSortByField) + " sort";

		setSqlNoOrderBy(sql_start + sql_middle + sql_end);
//        System.out.println("SQL Start " + sql_start);
//        System.out.println("SQL Middle " + sql_middle);
//        System.out.println("SQL End " + sql_end);
//        System.out.println("DbSortByField " + dbSortByField);
        
		setSql(sql_start + sql_middle + sql_end + " ORDER BY " + nvl(dbSortByField, "2"));
	} // updateParentSQL

	public boolean canUseSearchString() {
		return true;
	}

	public String getBaseSQL() {
		return "SELECT " + dbIdField + " FROM " + dbTableName;
	} // getBaseSQL

	public String getBaseWholeSQL() {
		return "SELECT " + dbIdField + " FROM " + dbTableName;
	} // getBaseSQL
	
	/*
	public void loadData(int pageNo) throws RaptorException {
		loadData(pageNo, "");
	} // loadData

	public void loadData(String pageNo) throws RaptorException {
		loadData(pageNo, "");
	} // loadData
*/
	
	public void loadData(String pageNo, String searchString, String dbInfo) throws RaptorException {
		int iPageNo = 0;

		if (pageNo != null)
			try {
				iPageNo = Integer.parseInt(pageNo);
			} catch (NumberFormatException e) {
			}

		loadData(iPageNo, searchString, dbInfo);
	} // loadData

	private void loadData(int pageNo, String searchString, String dbInfo) throws RaptorException {
		boolean dataAlreadyLoaded = (this.pageNo == pageNo)
				&& (this.searchString.equals(searchString));

		if (dataAlreadyLoaded)
			return;

		if (!this.searchString.equals(searchString)) {
			dataSize = -1;
			pageNo = 0;
		} // if

		this.pageNo = pageNo;
		this.searchString = searchString;
		updateParentSQL();
		performLoadData(searchString,dbInfo);
	} // loadData

} // IdNameLookup