/
PoRepoImpl.java
341 lines (299 loc) · 12.1 KB
/
PoRepoImpl.java
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/**
* (C) Copyright 2016 Johnson Controls, Inc
* Use or Copying of all or any part of this program, except as
* permitted by License Agreement, is prohibited.
*/
package com.jci.po.repo;
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Repository;
//import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jci.azure.DataHelper;
import com.jci.azure.DataUtil;
import com.jci.azure.PaginationParam;
import com.jci.azure.ResultSet;
import com.jci.azure.ScrollingParam;
import com.jci.config.AzureStorage;
import com.jci.entity.MiscDataEntity;
import com.jci.entity.PoEntity;
import com.jci.enums.ErrorEnum;
import com.jci.exception.ErrorService;
import com.jci.po.exception.PoException;
import com.jci.utils.Constants;
import com.jci.utils.QueryBuilder;
import com.microsoft.azure.storage.OperationContext;
import com.microsoft.azure.storage.ResultContinuation;
import com.microsoft.azure.storage.ResultSegment;
import com.microsoft.azure.storage.StorageException;
import com.microsoft.azure.storage.table.CloudTable;
import com.microsoft.azure.storage.table.DynamicTableEntity;
import com.microsoft.azure.storage.table.EntityProperty;
import com.microsoft.azure.storage.table.TableQuery;
/**
* <p>
* <strong> The Class PoRepoImpl.</strong>
* <p>
*
* @author csonisk
*/
@Repository
@RefreshScope
public class PoRepoImpl implements PoRepo { // NO_UCD (unused code)
/** The Constant LOG. */
private static final Logger LOG = LoggerFactory.getLogger(PoRepoImpl.class);
/** The all erps. */
@Value("${all.erp.names}")
private String allErps;
/** The batch size. */
final int batchSize = 10;
/** The azure storage. */
@Autowired
private AzureStorage azureStorage;
/** The error service. */
@Autowired
private ErrorService errorService;
/* (non-Javadoc)
* @see com.jci.po.repo.PoRepo#getGraphData()
*/
@Override
public HashMap<String, ArrayList<Integer>> getGraphData() {
String query = QueryBuilder.graphQuery(Constants.PARTITION_KEY_MISCDATA,allErps);
TableQuery<MiscDataEntity> partitionQuery = TableQuery.from(MiscDataEntity.class).where(query);
CloudTable cloudTable;
try {
cloudTable = azureStorage.getTable(Constants.TABLE_MISC);
} catch (InvalidKeyException | URISyntaxException | StorageException e) {
throw errorService.createException(PoException.class, e, ErrorEnum.ERROR_MISCDATA_TABLE_NOT_FOUND);
}
HashMap<String, ArrayList<Integer>> graphData = new HashMap<>();
ArrayList<Integer> list = null;
for (MiscDataEntity entity : cloudTable.execute(partitionQuery)) {
list = new ArrayList<>();
list.add(entity.getPoIntransitCount());
list.add(entity.getPoProcessedCount());
list.add(entity.getPoErrorCount());
graphData.put(entity.getRowKey(), list);
}
return graphData;
}
/**
* Gets the error data.
*
* @param partitionKey the partition key
* @return the error data
*/
public List<HashMap<String, String>> getErrorData(String partitionKey) {
List<HashMap<String, String>> errorData = new ArrayList<>();
String query = QueryBuilder.errorQuery(partitionKey,allErps);
TableQuery<PoEntity> partitionQuery = TableQuery.from(PoEntity.class).where(query);
//multiplePartitionWhereCondition
CloudTable cloudTable;
try {
cloudTable = azureStorage.getTable(Constants.TABLE_PO_DETAILS);
} catch (InvalidKeyException | URISyntaxException | StorageException e) {
throw errorService.createException(PoException.class, e, ErrorEnum.ERROR_PO_TABLE_NOT_FOUND);
}
for (PoEntity entity : cloudTable.execute(partitionQuery)) {
HashMap<String, String> map = new HashMap<>();
map.put("Status",String.valueOf(entity.getSupplierDeliveryState()));
map.put("Description",String.valueOf(entity.getDescription()));
map.put("OrderNumber",String.valueOf(entity.getRowKey()));
map.put("SourceErpName",String.valueOf(entity.getPartitionKey()));
errorData.add(map);
}
return errorData;
}
/* (non-Javadoc)
* @see com.jci.po.repo.PoRepo#getErrorPos(java.lang.String, java.util.List)
*/
@Override
public Map<String,List<HashMap<String, Object>>> getErrorPos(String partitionKey, List<String> poList) {
String query = QueryBuilder.getErrorPosQuery(partitionKey,poList);
CloudTable cloudTable;
try {
cloudTable = azureStorage.getTable(Constants.TABLE_PO_ITEM_DETAILS);
} catch (InvalidKeyException | URISyntaxException | StorageException e) {
throw errorService.createException(PoException.class, e, ErrorEnum.ERROR_POITEM_TABLE_NOT_FOUND);
}
OperationContext opContext = new OperationContext();
TableQuery<DynamicTableEntity> myQuery = TableQuery.from(DynamicTableEntity.class).where(query).take(1000);//Need to discuss this
Iterator<DynamicTableEntity> rows = cloudTable.execute(myQuery, null, opContext).iterator();
DynamicTableEntity row;
EntityProperty ep;
HashMap<String, Object> hashmap;
Map<String,List<HashMap<String, Object>>> poNumToItemListMap = new HashMap<>();
while(rows.hasNext()) {
row = rows.next() ;
HashMap<String, EntityProperty> map = row.getProperties();
hashmap = new HashMap<>();
for (String key : map.keySet()) {
ep = map.get(key);
hashmap.put(key, ep.getValueAsString());
}
if(poNumToItemListMap.containsKey(row.getRowKey().split("-")[0])){
List<HashMap<String, Object>> list =poNumToItemListMap.get(row.getRowKey().split("-")[0]);
list.add(hashmap);
poNumToItemListMap.put(row.getRowKey().split("-")[0], list);
}else{
List<HashMap<String, Object>> list = new ArrayList<>();
list.add(hashmap);
poNumToItemListMap.put(row.getRowKey().split("-")[0], list);
}
}
return poNumToItemListMap;
}
/* (non-Javadoc)
* @see com.jci.po.repo.PoRepo#getPoDetails(java.lang.String, java.util.List)
*/
@Override
public List<PoEntity> getPoDetails(String partitionKey, List<String> poList) {
String query = QueryBuilder.processPosQuery(partitionKey,poList);
List<PoEntity> errorData = new ArrayList<>();
TableQuery<PoEntity> partitionQuery = TableQuery.from(PoEntity.class).where(query);
CloudTable cloudTable;
try {
cloudTable = azureStorage.getTable(Constants.TABLE_PO_DETAILS);
} catch (InvalidKeyException | URISyntaxException | StorageException e) {
throw errorService.createException(PoException.class, e, ErrorEnum.ERROR_PO_TABLE_NOT_FOUND);
}
for (PoEntity entity : cloudTable.execute(partitionQuery)) {
errorData.add(entity);
}
return errorData;
}
/* (non-Javadoc)
* @see com.jci.po.repo.PoRepo#getSegmentedResultSet(com.jci.po.azure.query.ScrollingParam, com.jci.po.azure.data.DataHelper)
*/
@Override
public ResultSet getSegmentedResultSet(ScrollingParam param,DataHelper request) {
ResultContinuation continuationToken = DataUtil.getContinuationToken(param);
PaginationParam pagination = new PaginationParam();
if(continuationToken != null) {
pagination.setLastPartition(param.getPartition());
pagination.setLastRow(param.getRow());
}
// Create the query
String whereCondition = null;
if(request.isErrorDataRequired()){
whereCondition = QueryBuilder.errorDataQuery(request.getPartitionValue());
}else{
whereCondition = QueryBuilder.partitionWhereCondition(request.getPartitionValue());
}
if(StringUtils.isBlank(whereCondition) ){
return null;
}
TableQuery<DynamicTableEntity> query = TableQuery.from(DynamicTableEntity.class).where(whereCondition).take(param.getSize());
CloudTable table;
try {
table = azureStorage.getTable(request.getTableName());
} catch (InvalidKeyException | URISyntaxException | StorageException e) {
throw errorService.createException(PoException.class, e, ErrorEnum.ERROR_TABLE_NOT_FOUND,request.getTableName());
}
// segmented query
ResultSegment<DynamicTableEntity> response=null;
try {
response = table.executeSegmented(query, continuationToken);
} catch (StorageException e) {
throw errorService.createException(PoException.class, e, ErrorEnum.ERROR_PO_SEGMENTED_QUERY, query);
}
// next continuation token
continuationToken = response.getContinuationToken() ;
if(continuationToken != null) {
pagination.setNextPartition(continuationToken.getNextPartitionKey());
pagination.setNextRow(continuationToken.getNextRowKey());
}
HashMap<String, Object> hashmap;
List<HashMap<String, Object>> series = new ArrayList<>();
DynamicTableEntity row;
EntityProperty ep;
Iterator<DynamicTableEntity> rows = response.getResults().iterator() ;
while(rows.hasNext()) {
row = rows.next() ;
HashMap<String, EntityProperty> map = row.getProperties();
hashmap = new HashMap<>();
hashmap.put("id", row.getRowKey());
hashmap.put("OrderNumber", row.getRowKey());
for (String key : map.keySet()) {
ep = map.get(key);
if(key.equals(Constants.STATUS_KEY)){
hashmap.put("Status", ep.getValueAsString());
}
}
series.add(hashmap);
}
return new ResultSet(series,pagination) ;
}
/* (non-Javadoc)
* @see com.jci.po.repo.PoRepo#getPoItemDetail(com.jci.po.azure.query.ScrollingParam, com.jci.po.azure.data.DataHelper)
*/
@Override
public ResultSet getPoItemDetail(ScrollingParam param,DataHelper request) {
ResultContinuation continuationToken = DataUtil.getContinuationToken(param);
PaginationParam pagination = new PaginationParam();
if(continuationToken != null) {
pagination.setLastPartition(param.getPartition());
pagination.setLastRow(param.getRow());
}
// Create the query
String whereCondition = QueryBuilder.poItemDetailQuery(request);
if(StringUtils.isBlank(whereCondition) ){
return null;
}
TableQuery<DynamicTableEntity> query = TableQuery.from(DynamicTableEntity.class).where(whereCondition).take(param.getSize());
CloudTable table;
try {
table = azureStorage.getTable(request.getTableName());
} catch (InvalidKeyException | URISyntaxException | StorageException e1) {
throw errorService.createException(PoException.class, e1, ErrorEnum.ERROR_TABLE_NOT_FOUND,request.getTableName());
}
// segmented query
ResultSegment<DynamicTableEntity> response=null;
try {
response = table.executeSegmented(query, continuationToken);
} catch (StorageException e1) {
throw errorService.createException(PoException.class, e1, ErrorEnum.ERROR_POITEM_SEGMENTED_QUERY,query);
}
// next continuation token
continuationToken = response.getContinuationToken() ;
if(continuationToken != null) {
pagination.setNextPartition(continuationToken.getNextPartitionKey());
pagination.setNextRow(continuationToken.getNextRowKey());
}
HashMap<String, Object> hashmap;
List<HashMap<String, Object>> series = new ArrayList<>();
DynamicTableEntity row;
ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>() {};
Iterator<DynamicTableEntity> rows = response.getResults().iterator() ;
while(rows.hasNext()) {
row = rows.next() ;
HashMap<String, EntityProperty> map = row.getProperties();
hashmap = new HashMap<>();
String jsonString = map.get(Constants.JSON_STRING).getValueAsString();
if(!StringUtils.isBlank(jsonString)){
try {
hashmap = mapper.readValue(jsonString, typeRef);
hashmap.put("id", row.getRowKey());
} catch (IOException e) {
LOG.error("### Exception in ####",e);
}
}
series.add(hashmap);
}
return new ResultSet(series,pagination) ;
}
}