[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / DashboardSearchServiceImpl.java
1 /*-\r
2  * ================================================================================\r
3  * ECOMP Portal\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ================================================================================\r
19  */\r
20 package org.openecomp.portalapp.portal.service;\r
21 \r
22 import java.util.ArrayList;\r
23 import java.util.HashMap;\r
24 import java.util.List;\r
25 import java.util.Map;\r
26 \r
27 import org.springframework.beans.factory.annotation.Autowired;\r
28 import org.springframework.stereotype.Component;\r
29 \r
30 import org.openecomp.portalsdk.core.service.DataAccessService;\r
31 import org.openecomp.portalapp.portal.transport.CommonWidget;\r
32 import org.openecomp.portalapp.portal.transport.CommonWidgetMeta;\r
33 import org.openecomp.portalapp.portal.ecomp.model.SearchResultItem;\r
34 \r
35 @Component\r
36 public class DashboardSearchServiceImpl implements DashboardSearchService {\r
37 \r
38         @Autowired\r
39         private DataAccessService dataAccessService;\r
40 \r
41         public Map<String, List<SearchResultItem>> searchResults(String userId, String searchString) {\r
42                 Map<String, String> params = new HashMap<>();\r
43                 params.put("userId", userId);\r
44                 params.put("searchQuery", searchString);\r
45                 // Named query is stored in a *.hbm.xml file, mapped to SearchResultItem\r
46                 @SuppressWarnings("unchecked")\r
47                 List<SearchResultItem> list = dataAccessService.executeNamedQuery("searchPortal", params, null);\r
48                 Map<String, List<SearchResultItem>> finalJson = null;\r
49                 if (list.size() > 0) {\r
50                         finalJson = new HashMap<String, List<SearchResultItem>>();\r
51                         for (SearchResultItem thisResult : list) {\r
52                                 List<SearchResultItem> thisList = finalJson.get(thisResult.getCategory().toLowerCase());\r
53                                 if (thisList == null)\r
54                                         thisList = new ArrayList<SearchResultItem>();\r
55                                 thisList.add(thisResult);\r
56                                 finalJson.put(thisResult.getCategory().toLowerCase(), thisList);\r
57                         }\r
58                 }\r
59                 return finalJson;\r
60         }\r
61 \r
62         @Override\r
63         public List<String> getRelatedUsers(String userId) {\r
64                 Map<String, String> params = new HashMap<>();\r
65                 params.put("userId", userId);\r
66                 @SuppressWarnings("unchecked")\r
67                 List<String> activeUsers = dataAccessService.executeNamedQuery("relatedUsers", params, null);\r
68                 return activeUsers;\r
69         }\r
70 \r
71         @Override\r
72         public CommonWidgetMeta getWidgetData(String resourceType) {\r
73                 Map<String, String> params = new HashMap<>();\r
74                 params.put("cat", resourceType);\r
75                 @SuppressWarnings("unchecked")\r
76                 List<CommonWidget> widgetItems = (List<CommonWidget>) dataAccessService.executeNamedQuery("getCommonWidgetItem", params, null);\r
77                 return new CommonWidgetMeta(resourceType, widgetItems);\r
78         }\r
79 \r
80         @Override\r
81         public String saveWidgetDataBulk(CommonWidgetMeta commonMetaWidgetData) {\r
82                 for (CommonWidget widgetData : commonMetaWidgetData.getItems()) {\r
83                         widgetData.setCategory(commonMetaWidgetData.getCategory());\r
84                         dataAccessService.saveDomainObject(widgetData, null);\r
85                 }\r
86                 return "success";\r
87         }\r
88 \r
89         @Override\r
90         public String saveWidgetData(CommonWidget commonWidgetData) {\r
91                 dataAccessService.saveDomainObject(commonWidgetData, null);\r
92                 return "success";\r
93         }\r
94 \r
95         @Override\r
96         public String deleteWidgetData(CommonWidget eventWidget) {\r
97                 dataAccessService.deleteDomainObject(eventWidget, null);\r
98                 return "success";\r
99         }\r
100 }\r