Security/ Package Name changes
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / utils / HashMapFromList.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.portal.utils;
39
40 import java.lang.reflect.Field;
41 import java.lang.reflect.Method;
42 import java.util.HashMap;
43 import java.util.List;
44
45 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
46
47 /**
48  * 
49  * @author Vladimir Turovets This class is used to create HashMap<String, T>
50  *         from the list of T objects. We suppose that
51  *         1) T object contains field 'parmName' or getter 'getParmName()'.
52  *         The value of object.parmName or object.getParmName() is used as
53  *         a key in created hashMap.
54  *         2) for all objects in the list 'parmName' or getter
55  *         'getParmName().toString' has to be unique and not null.
56  *         This class has one function only:
57  *         HashMap<String, T> hashMap(List<T> list, String name) and returns
58  *         hash map created from list.
59  *
60  * @param <T>
61  * Type 
62  */
63 public class HashMapFromList<T> {
64         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HashMapFromList.class);
65         
66         public HashMap<String, T> hashMap(List<T> list, String name) {
67                 HashMap<String, T> result = new HashMap<String, T>();
68                 if (list == null || list.size() == 0 || name == null) {
69                         return result;
70                 }
71                 name = name.trim();
72                 if (name.length() > 0) {
73                         T object = list.get(0);
74                         try {
75                                 String parmName = name;
76                                 Field field = object.getClass().getField(parmName);
77                                 for (T obj : list) {
78                                         try {
79                                                 Object o = field.get(obj);
80                                                 if (o != null)
81                                                         result.put(o.toString(), obj);
82                                         } catch (Exception e1) {
83                                                 logger.error(EELFLoggerDelegate.errorLogger, "hashMap failed 1: object of class " + object.getClass().getName() + ", field " + parmName, e1);
84                                                 return new HashMap<String, T>();
85                                         }
86                                 }
87                         } catch (Exception e) {
88                                 String getterName = "get" + (name.length() == 1 ? name.toUpperCase() : (name.substring(0, 1).toUpperCase() + name.substring(1)));
89                                 try {
90                                         Class<?>[] parmClasses = null;
91                                         Method method = object.getClass().getMethod(getterName, parmClasses);
92                                         Object[] parmValues = new Object[0];
93                                         for (T obj : list) {
94                                                 try {
95                                                         Object o = method.invoke(obj, parmValues);
96                                                         if (o != null)
97                                                                 result.put(o.toString(), obj);
98                                                 } catch (Exception e2) {
99                                                         logger.error(EELFLoggerDelegate.errorLogger, "hashMap failed 2: object of class " + object.getClass().getName() + ", method " + getterName, e2);
100                                                         return new HashMap<String, T>();
101                                                 }
102                                         }
103                                 } catch (Exception e3) {
104                                         logger.error(EELFLoggerDelegate.errorLogger, "hashMap failed 3: object of class " + object.getClass().getName() + ", bad field '" + name + "' or method '" + getterName + "()", e3);
105                                         return new HashMap<String, T>();
106                                 }
107                         }
108                 }
109                 if (list.size() != result.size()) {
110                         logger.warn(EELFLoggerDelegate.errorLogger, "Duplicated or empty keys were found!!!");
111                 }
112                 return result;
113         }
114
115 }