Deliver centralized role management feature
[portal.git] / ecomp-portal-widget-ms / widget-ms / src / main / java / org / openecomp / portalapp / widget / service / impl / InitializationServiceImpl.java
@@ -8,7 +8,6 @@ import java.util.HashSet;
 
 import javax.transaction.Transactional;
 
-import org.openecomp.portalapp.widget.controller.WidgetsCatalogController;
 import org.openecomp.portalapp.widget.domain.MicroserviceData;
 import org.openecomp.portalapp.widget.domain.MicroserviceParameter;
 import org.openecomp.portalapp.widget.domain.RoleApp;
@@ -24,49 +23,51 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.EnableAspectJAutoProxy;
 import org.springframework.stereotype.Service;
 
+/**
+ * Uploads widget zip archives to Portal.
+ */
 @Service("initService")
 @Transactional
 @org.springframework.context.annotation.Configuration
 @EnableAspectJAutoProxy
-public class InitializationServiceImpl implements InitializationService{
-       
+public class InitializationServiceImpl implements InitializationService {
+
        private static final String BASIC_AUTH = "Basic Authentication";
        private static final String PARAMETER_KEY = "resourceType";
-       private static final Logger logger = LoggerFactory.getLogger(WidgetsCatalogController.class);
-       
+       private static final Logger logger = LoggerFactory.getLogger(InitializationServiceImpl.class);
+
        @Autowired
        WidgetCatalogService widgetCatalogService;
-       
+
        @Autowired
        StorageService storageService;
-       
+
        @Autowired
        MicroserviceService microserviceService;
-       
+
        @Value("${account.user.name}")
        String account_user;
-       
+
        @Value("${account.user.password}")
        String account_password;
-       
+
        @Value("${initialization.widgetData.url}")
        String widgetData_url;
-       
+
        @Override
        public void initialize() {
                initCommonWidget("News");
                initCommonWidget("Events");
                initCommonWidget("Resources");
        }
-       
-       private void initCommonWidget(String name){
-               
+
+       private void initCommonWidget(String name) {
+
                final String newServiceName = name + " Microservice";
-               final String newWidgetName = name + " Widget";
-               
+
                Long serviceId = microserviceService.getMicroserviceIdByName(newServiceName);
-       
-               if(serviceId == null){
+
+               if (serviceId == null) {
                        MicroserviceData newService = new MicroserviceData();
                        newService.setName(newServiceName);
                        newService.setDesc(name);
@@ -77,30 +78,29 @@ public class InitializationServiceImpl implements InitializationService{
                        newService.setPassword(account_password);
                        newService.setActive("Y");
                        serviceId = microserviceService.saveMicroserivce(newService);
-                       
-                       
+
                        MicroserviceParameter parameter = new MicroserviceParameter();
                        parameter.setServiceId(serviceId);
                        parameter.setPara_key(PARAMETER_KEY);
                        String parameter_value = null;
-                       switch(name.toLowerCase()){
-                               case "news":
-                                       parameter_value = "NEWS";
-                                       break;
-                               case "events":
-                                       parameter_value = "EVENTS";
-                                       break;
-                               case "resources":
-                                       parameter_value = "IMPORTANTRESOURCES";
-                                       break;
+                       switch (name.toLowerCase()) {
+                       case "news":
+                               parameter_value = "NEWS";
+                               break;
+                       case "events":
+                               parameter_value = "EVENTS";
+                               break;
+                       case "resources":
+                               parameter_value = "IMPORTANTRESOURCES";
+                               break;
                        }
                        parameter.setPara_value(parameter_value);
                        microserviceService.saveMicroserviceParameter(parameter);
                }
-               
-               if(!widgetCatalogService.getWidgetIdByName(newWidgetName)){
+
+               if (!widgetCatalogService.getWidgetIdByName(name)) {
                        WidgetCatalog newWidget = new WidgetCatalog();
-                       newWidget.setName(newWidgetName);
+                       newWidget.setName(name);
                        newWidget.setDesc(name);
                        newWidget.setAllowAllUser("1");
                        String fileLocation = name.toLowerCase() + "-widget.zip";
@@ -108,21 +108,26 @@ public class InitializationServiceImpl implements InitializationService{
                        newWidget.setServiceId(serviceId);
                        newWidget.setWidgetRoles(new HashSet<RoleApp>());
                        long widgetId = widgetCatalogService.saveWidgetCatalog(newWidget);
-       
-                       File file = new File("/tmp/" + fileLocation);
-                       try{
-                               
-                               InputStream fileInputStream = this.getClass().getClassLoader().getResourceAsStream(fileLocation);
-                               OutputStream outputStream = new FileOutputStream(file);
+
+                       File tmpZipFile = new File("/tmp/" + fileLocation);
+                       InputStream fileInputStream = null;
+                       OutputStream outputStream = null;
+                       try {
+                               fileInputStream = this.getClass().getClassLoader().getResourceAsStream(fileLocation);
+                               outputStream = new FileOutputStream(tmpZipFile);
                                int read = 0;
                                byte[] bytes = new byte[4096];
                                while ((read = fileInputStream.read(bytes)) != -1) {
                                        outputStream.write(bytes, 0, read);
                                }
-                       }catch(Exception e){
-                               logger.error("Exception occurred while performing InitializationServiceImpl.initCommonWidget in widget microservices. Details:" + e.getMessage());
+                               outputStream.close();
+                               fileInputStream.close();
+                       } catch (Exception e) {
+                               logger.error(
+                                               "Exception occurred while performing InitializationServiceImpl.initCommonWidget in widget microservices. Details:", e);
                        }
-                       storageService.initSave(file, newWidget, widgetId);     
+                       storageService.initSave(tmpZipFile, newWidget, widgetId);
+                       tmpZipFile.delete();
                }
        }
 }