[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / DashboardSearchServiceImpl.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.service;
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Component;
29
30 import org.openecomp.portalsdk.core.service.DataAccessService;
31 import org.openecomp.portalapp.portal.transport.CommonWidget;
32 import org.openecomp.portalapp.portal.transport.CommonWidgetMeta;
33 import org.openecomp.portalapp.portal.ecomp.model.SearchResultItem;
34
35 @Component
36 public class DashboardSearchServiceImpl implements DashboardSearchService {
37
38         @Autowired
39         private DataAccessService dataAccessService;
40
41         public Map<String, List<SearchResultItem>> searchResults(String userId, String searchString) {
42                 Map<String, String> params = new HashMap<>();
43                 params.put("userId", userId);
44                 params.put("searchQuery", searchString);
45                 // Named query is stored in a *.hbm.xml file, mapped to SearchResultItem
46                 @SuppressWarnings("unchecked")
47                 List<SearchResultItem> list = dataAccessService.executeNamedQuery("searchPortal", params, null);
48                 Map<String, List<SearchResultItem>> finalJson = null;
49                 if (list.size() > 0) {
50                         finalJson = new HashMap<String, List<SearchResultItem>>();
51                         for (SearchResultItem thisResult : list) {
52                                 List<SearchResultItem> thisList = finalJson.get(thisResult.getCategory().toLowerCase());
53                                 if (thisList == null)
54                                         thisList = new ArrayList<SearchResultItem>();
55                                 thisList.add(thisResult);
56                                 finalJson.put(thisResult.getCategory().toLowerCase(), thisList);
57                         }
58                 }
59                 return finalJson;
60         }
61
62         @Override
63         public List<String> getRelatedUsers(String userId) {
64                 Map<String, String> params = new HashMap<>();
65                 params.put("userId", userId);
66                 @SuppressWarnings("unchecked")
67                 List<String> activeUsers = dataAccessService.executeNamedQuery("relatedUsers", params, null);
68                 return activeUsers;
69         }
70
71         @Override
72         public CommonWidgetMeta getWidgetData(String resourceType) {
73                 Map<String, String> params = new HashMap<>();
74                 params.put("cat", resourceType);
75                 @SuppressWarnings("unchecked")
76                 List<CommonWidget> widgetItems = (List<CommonWidget>) dataAccessService.executeNamedQuery("getCommonWidgetItem", params, null);
77                 return new CommonWidgetMeta(resourceType, widgetItems);
78         }
79
80         @Override
81         public String saveWidgetDataBulk(CommonWidgetMeta commonMetaWidgetData) {
82                 for (CommonWidget widgetData : commonMetaWidgetData.getItems()) {
83                         widgetData.setCategory(commonMetaWidgetData.getCategory());
84                         dataAccessService.saveDomainObject(widgetData, null);
85                 }
86                 return "success";
87         }
88
89         @Override
90         public String saveWidgetData(CommonWidget commonWidgetData) {
91                 dataAccessService.saveDomainObject(commonWidgetData, null);
92                 return "success";
93         }
94
95         @Override
96         public String deleteWidgetData(CommonWidget eventWidget) {
97                 dataAccessService.deleteDomainObject(eventWidget, null);
98                 return "success";
99         }
100 }