nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / portal / utils / HashMapFromList.java
1 /*-
2  * ================================================================================
3  * eCOMP Portal
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.portalapp.portal.utils;
21
22 import java.lang.reflect.Field;
23 import java.lang.reflect.Method;
24 import java.util.HashMap;
25 import java.util.List;
26
27 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
28
29 /**
30  * 
31  * @author Vladimir Turovets This class is used to create HashMap<String, T>
32  *         from the list of T objects. We suppose that
33  *         1) T object contains field 'parmName' or getter 'getParmName()'.
34  *         The value of object.parmName or object.getParmName() is used as
35  *         a key in created hashMap.
36  *         2) for all objects in the list 'parmName' or getter
37  *         'getParmName().toString' has to be unique and not null.
38  *         This class has one function only:
39  *         HashMap<String, T> hashMap(List<T> list, String name) and returns
40  *         hash map created from list.
41  *
42  * @param <T>
43  */
44 public class HashMapFromList<T> {
45         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(HashMapFromList.class);
46         
47         public HashMap<String, T> hashMap(List<T> list, String name) {
48                 HashMap<String, T> result = new HashMap<String, T>();
49                 if (list == null || list.size() == 0 || name == null) {
50                         return result;
51                 }
52                 name = name.trim();
53                 if (name.length() > 0) {
54                         T object = list.get(0);
55                         try {
56                                 String parmName = name;
57                                 Field field = object.getClass().getField(parmName);
58                                 for (T obj : list) {
59                                         try {
60                                                 Object o = field.get(obj);
61                                                 if (o != null)
62                                                         result.put(o.toString(), obj);
63                                         } catch (Exception e1) {
64                                                 String stackTrace = EcompPortalUtils.getStackTrace(e1);
65                                                 logger.error(EELFLoggerDelegate.errorLogger, "Object of class " + object.getClass().getName() + ", field " + parmName + ". Details: ", stackTrace);
66                                                 return new HashMap<String, T>();
67                                         }
68                                 }
69                         } catch (Exception e) {
70                                 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
71                                 String getterName = "get" + (name.length() == 1 ? name.toUpperCase() : (name.substring(0, 1).toUpperCase() + name.substring(1)));
72                                 try {
73                                         Class<?>[] parmClasses = null;
74                                         Method method = object.getClass().getMethod(getterName, parmClasses);
75                                         Object[] parmValues = new Object[0];
76                                         for (T obj : list) {
77                                                 try {
78                                                         Object o = method.invoke(obj, parmValues);
79                                                         if (o != null)
80                                                                 result.put(o.toString(), obj);
81                                                 } catch (Exception e2) {
82                                                         String stackTrace = EcompPortalUtils.getStackTrace(e2);
83                                                         logger.error(EELFLoggerDelegate.errorLogger, "Object of class " + object.getClass().getName() + ", method " + getterName + ". Details: ", stackTrace);
84                                                         return new HashMap<String, T>();
85                                                 }
86                                         }
87                                 } catch (Exception e3) {
88                                         String stackTrace = EcompPortalUtils.getStackTrace(e3);
89                                         logger.error(EELFLoggerDelegate.errorLogger, "Object of class " + object.getClass().getName() + ", bad field '" + name + "' or method '" + getterName + "()'. Details: " + stackTrace);
90                                         return new HashMap<String, T>();
91                                 }
92                         }
93                 }
94                 if (list.size() != result.size()) {
95                         logger.warn(EELFLoggerDelegate.errorLogger, "Duplicated or empty keys were found!!!");
96                 }
97                 return result;
98         }
99
100 }