[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-widget-ms / src / main / java / org / openecomp / portalapp / widget / service / impl / InitializationServiceImpl.java
1 package org.openecomp.portalapp.widget.service.impl;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.InputStream;
6 import java.io.OutputStream;
7 import java.util.HashSet;
8
9 import javax.transaction.Transactional;
10
11 import org.openecomp.portalapp.widget.controller.WidgetsCatalogController;
12 import org.openecomp.portalapp.widget.domain.MicroserviceData;
13 import org.openecomp.portalapp.widget.domain.MicroserviceParameter;
14 import org.openecomp.portalapp.widget.domain.RoleApp;
15 import org.openecomp.portalapp.widget.domain.WidgetCatalog;
16 import org.openecomp.portalapp.widget.service.InitializationService;
17 import org.openecomp.portalapp.widget.service.MicroserviceService;
18 import org.openecomp.portalapp.widget.service.StorageService;
19 import org.openecomp.portalapp.widget.service.WidgetCatalogService;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.beans.factory.annotation.Value;
24 import org.springframework.context.annotation.EnableAspectJAutoProxy;
25 import org.springframework.stereotype.Service;
26
27 @Service("initService")
28 @Transactional
29 @org.springframework.context.annotation.Configuration
30 @EnableAspectJAutoProxy
31 public class InitializationServiceImpl implements InitializationService{
32         
33         private static final String BASIC_AUTH = "Basic Authentication";
34         private static final String PARAMETER_KEY = "resourceType";
35         private static final Logger logger = LoggerFactory.getLogger(WidgetsCatalogController.class);
36         
37         @Autowired
38         WidgetCatalogService widgetCatalogService;
39         
40         @Autowired
41         StorageService storageService;
42         
43         @Autowired
44         MicroserviceService microserviceService;
45         
46         @Value("${account.user.name}")
47         String account_user;
48         
49         @Value("${account.user.password}")
50         String account_password;
51         
52         @Value("${initialization.widgetData.url}")
53         String widgetData_url;
54         
55         @Override
56         public void initialize() {
57                 initCommonWidget("News");
58                 initCommonWidget("Events");
59                 initCommonWidget("Resources");
60         }
61         
62         private void initCommonWidget(String name){
63                 
64                 final String newServiceName = name + " Microservice";
65                 final String newWidgetName = name + " Widget";
66                 
67                 Long serviceId = microserviceService.getMicroserviceIdByName(newServiceName);
68         
69                 if(serviceId == null){
70                         MicroserviceData newService = new MicroserviceData();
71                         newService.setName(newServiceName);
72                         newService.setDesc(name);
73                         newService.setAppId(1);
74                         newService.setUrl(widgetData_url);
75                         newService.setSecurityType(BASIC_AUTH);
76                         newService.setUsername(account_user);
77                         newService.setPassword(account_password);
78                         newService.setActive("Y");
79                         serviceId = microserviceService.saveMicroserivce(newService);
80                         
81                         
82                         MicroserviceParameter parameter = new MicroserviceParameter();
83                         parameter.setServiceId(serviceId);
84                         parameter.setPara_key(PARAMETER_KEY);
85                         String parameter_value = null;
86                         switch(name.toLowerCase()){
87                                 case "news":
88                                         parameter_value = "NEWS";
89                                         break;
90                                 case "events":
91                                         parameter_value = "EVENTS";
92                                         break;
93                                 case "resources":
94                                         parameter_value = "IMPORTANTRESOURCES";
95                                         break;
96                         }
97                         parameter.setPara_value(parameter_value);
98                         microserviceService.saveMicroserviceParameter(parameter);
99                 }
100                 
101                 if(!widgetCatalogService.getWidgetIdByName(newWidgetName)){
102                         WidgetCatalog newWidget = new WidgetCatalog();
103                         newWidget.setName(newWidgetName);
104                         newWidget.setDesc(name);
105                         newWidget.setAllowAllUser("1");
106                         String fileLocation = name.toLowerCase() + "-widget.zip";
107                         newWidget.setFileLocation(fileLocation);
108                         newWidget.setServiceId(serviceId);
109                         newWidget.setWidgetRoles(new HashSet<RoleApp>());
110                         long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);
111         
112                         File file = new File("/tmp/" + fileLocation);
113                         try{
114                                 
115                                 InputStream fileInputStream = this.getClass().getClassLoader().getResourceAsStream(fileLocation);
116                                 OutputStream outputStream = new FileOutputStream(file);
117                                 int read = 0;
118                                 byte[] bytes = new byte[4096];
119                                 while ((read = fileInputStream.read(bytes)) != -1) {
120                                         outputStream.write(bytes, 0, read);
121                                 }
122                         }catch(Exception e){
123                                 logger.error("Exception occurred while performing InitializationServiceImpl.initCommonWidget in widget microservices. Details:" + e.getMessage());
124                         }
125                         storageService.initSave(file, newWidget, widgetId);     
126                 }
127         }
128 }