summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-widget-ms/widget-ms/src/main/java
diff options
context:
space:
mode:
authorjz385p <jegadeesh.babu@att.com>2020-07-27 19:15:41 +0530
committerJegadeesh Babu <jegadeesh.babu@att.com>2020-07-27 13:54:21 +0000
commit0547751f8b0b8b5a3e96fb04c266e23884f97b85 (patch)
treeb6f559308b83f1653d348a75f3b4f6e593e67245 /ecomp-portal-widget-ms/widget-ms/src/main/java
parent7945565f9363846d0a8864217c2f77401960208c (diff)
widget changes to upload single JS file
widgetMS changes to upload single File Issue-ID: PORTAL-961 Change-Id: I20c2fd5c00e0dccab354b0a1c063b1a395955590 Signed-off-by: jz385p <jegadeesh.babu@att.com>
Diffstat (limited to 'ecomp-portal-widget-ms/widget-ms/src/main/java')
-rw-r--r--ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/controller/WidgetsCatalogController.java3
-rw-r--r--ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/StorageService.java2
-rw-r--r--ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/StorageServiceImpl.java135
-rw-r--r--ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/WidgetCatalogServiceImpl.java2
-rw-r--r--ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java3
5 files changed, 95 insertions, 50 deletions
diff --git a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/controller/WidgetsCatalogController.java b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/controller/WidgetsCatalogController.java
index 1896a4f8..e6c1c0ac 100644
--- a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/controller/WidgetsCatalogController.java
+++ b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/controller/WidgetsCatalogController.java
@@ -163,13 +163,14 @@ public class WidgetsCatalogController {
try {
//check the zip file structure first
respond = storageService.checkZipFile(file);
+ logger.debug("Check file validity"+respond.isValid()+respond.getError());
if(respond.isValid()){
//update the widget catalog
WidgetCatalog newWidget = new ObjectMapper().readValue(widget, WidgetCatalog.class);
widgetCatalogService.updateWidgetCatalog(widgetId, newWidget);
logger.debug("WidgetsCatalogController.saveWidgetCatalog: updating widget with widgetId={}", widgetId);
//update the widget zip file
- storageService.update(file, newWidget, widgetId);
+ storageService.updateJsFile(file, newWidget, widgetId);
}
} catch (Exception e) {
logger.error("Exception occurred while performing WidgetsCatalogController.saveWidgetCatalog in widget microservices. Details:", e);
diff --git a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/StorageService.java b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/StorageService.java
index fbd0f963..d8b56422 100644
--- a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/StorageService.java
+++ b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/StorageService.java
@@ -30,5 +30,7 @@ public interface StorageService {
void update(MultipartFile file, WidgetCatalog newWidget, long widgetId);
+ void updateJsFile(MultipartFile file, WidgetCatalog newWidget, long widgetId);
+
byte[] getWidgetCatalogContent(long widgetId) throws Exception;
}
diff --git a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/StorageServiceImpl.java b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/StorageServiceImpl.java
index 7a35ba4e..3bb41b6b 100644
--- a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/StorageServiceImpl.java
+++ b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/StorageServiceImpl.java
@@ -73,7 +73,7 @@ public class StorageServiceImpl implements StorageService {
Criteria criteria = session.createCriteria(WidgetFile.class);
criteria.add(Restrictions.eq("widgetId", widgetId));
List<WidgetFile> widgetFiles = criteria.list();
- session.flush();
+ //session.flush();
session.close();
if (widgetFiles.size() > 0)
widgetFile = widgetFiles.get(0);
@@ -148,7 +148,7 @@ public class StorageServiceImpl implements StorageService {
logger.error("StorageServiceImpl.save: Failed to store file " + file.getOriginalFilename(), e);
throw new StorageException("Failed to store file " + file.getOriginalFilename(), e);
}
- saveHelper(newWidget, widgetId, map);
+ saveJsHelper(newWidget, widgetId, map);
}
@Override
@@ -167,7 +167,7 @@ public class StorageServiceImpl implements StorageService {
throw new StorageException("Failed to store file " + file.getName(), e);
}
- saveHelper(newWidget, widgetId, map);
+ saveJsHelper(newWidget, widgetId, map);
}
/**
@@ -259,6 +259,90 @@ public class StorageServiceImpl implements StorageService {
widgetId);
}
+
+ /**
+ * Helper method to UnZip File
+ *
+ * @param file
+ */
+ private Map<String, byte[]> unZipFile(MultipartFile file) {
+ UnzipUtil unzipper = new UnzipUtil();
+ Map<String, byte[]> map;
+ File convFile;
+ try {
+ if (file.isEmpty()) {
+ logger.error("StorageServiceImpl.update: Failed to store empty file " + file.getOriginalFilename());
+ throw new StorageException("Failed to store empty file " + file.getOriginalFilename());
+ }
+ String fileLocation = file.getOriginalFilename();
+ logger.debug("StorageServiceImpl.update: store the widget to:" + fileLocation);
+ convFile = new File(fileLocation);
+ try(FileOutputStream fos = new FileOutputStream(convFile)){
+ fos.write(file.getBytes());
+ }
+ map = unzipper.unzip_db(fileLocation, ".", "tempWidgets");
+ convFile.delete();
+ return map;
+ } catch (IOException e) {
+ logger.error("StorageServiceImpl.update: Failed to store file " + file.getOriginalFilename(), e);
+ throw new StorageException("StorageServiceImpl.update: Failed to store file " + file.getOriginalFilename(),
+ e);
+ }
+ }
+
+ /**
+ * Helper method for saving widget file (controller.js) to ep_widget_catalog_files table in database
+ *
+ * @param newWidget
+ * @param widgetId
+ * @param map
+ */
+ private void saveJsHelper(WidgetCatalog newWidget, long widgetId, Map<String, byte[]> map) {
+
+ logger.debug("Going to save controller.js " + newWidget);
+ WidgetFile widgetFile = new WidgetFile();
+ widgetFile.setName(newWidget.getName());
+ widgetFile.setWidgetId(widgetId);
+ final byte[] controllerLoc = map.get(WidgetConstant.WIDGET_CONTROLLER_LOCATION);
+ if (controllerLoc == null || controllerLoc.length == 0)
+ throw new IllegalArgumentException(
+ "Map is missing required key " + WidgetConstant.WIDGET_CONTROLLER_LOCATION);
+ String javascript = new String(controllerLoc);
+
+ widgetFile.setController(javascript.getBytes());
+ Session session = sessionFactory.openSession();
+ session.save(widgetFile);
+ session.flush();
+ session.close();
+ logger.debug(
+ "StorageServiceImpl.save: saved controller.js file to the database for widget {}",
+ widgetId);
+
+ }
+
+ @Override
+ public void updateJsFile(MultipartFile file, WidgetCatalog newWidget, long widgetId) {
+ Map<String, byte[]> map;
+ map = unZipFile(file);
+ //Get existing widget file from DB
+ WidgetFile widgetFile = getWidgetFile(widgetId);
+
+ String javascript = new String(map.get(WidgetConstant.WIDGET_CONTROLLER_LOCATION));
+ widgetFile.setController(javascript.getBytes());
+ Session session = sessionFactory.openSession();
+ Transaction tx = session.beginTransaction();
+ session.update(widgetFile);
+ tx.commit();
+ session.flush();
+ session.close();
+ logger.debug(
+ "StorageServiceImpl.save: updated controller.js file to the database for widget {}",
+ widgetId);
+ }
+
+
+
+
@Override
public void update(MultipartFile file, WidgetCatalog newWidget, long widgetId) {
@@ -424,53 +508,14 @@ public class StorageServiceImpl implements StorageService {
public byte[] getWidgetCatalogContent(long widgetId) throws Exception {
WidgetCatalog widget = widgetCatalogService.getWidgetCatalog(widgetId);
- String namespace = "Portal" + widgetId + "Widget";
- String controllerName = "Portal" + widgetId + "Ctrl";
- String cssName = "portal" + widgetId + "-css-ready";
-
- String styles = getWidgetCSS(widgetId).replaceAll(cssName, widget.getName() + "-css-ready");
File f = File.createTempFile("temp", ".zip");
try(ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f))){
- ZipEntry e = new ZipEntry(widget.getName() + "/styles/styles.css");
- out.putNextEntry(new ZipEntry(widget.getName() + "/"));
- out.putNextEntry(new ZipEntry(widget.getName() + "/styles/"));
- out.putNextEntry(e);
- byte[] data = styles.getBytes();
- out.write(data, 0, data.length);
-
- String widgetData = namespace + "=" + namespace + "||{};" + "var res = " + namespace + ".widgetData;";
- String javascript = getWidgetController(widgetId).replace(widgetData, "").replace(namespace + ".controller =",
- "");
-
- String functionHeader = javascript.substring(javascript.indexOf("function"), javascript.indexOf(")") + 1);
- javascript = javascript.replaceFirst(controllerName, widget.getName() + "Ctrl");
- String functionParam = functionHeader.substring(functionHeader.indexOf("(") + 1, functionHeader.indexOf(")"));
- StringBuilder injectStr = new StringBuilder().append("[");
- List<String> paramList = Arrays.asList(functionParam.split(","));
- for (int i = 0; i < paramList.size(); i++) {
- if (i == paramList.size() - 1)
- injectStr.append("'" + paramList.get(i).trim() + "'];");
- else
- injectStr.append("'" + paramList.get(i).trim() + "',");
- }
- javascript = javascript.replace(";" + namespace + ".controller.$inject = " + injectStr.toString(), "");
+ String javascript = getWidgetController(widgetId);
- e = new ZipEntry(widget.getName() + "/js/controller.js");
+ ZipEntry e = new ZipEntry(widget.getName() + "/js/controller.js");
out.putNextEntry(new ZipEntry(widget.getName() + "/js/"));
out.putNextEntry(e);
- data = javascript.getBytes();
- out.write(data, 0, data.length);
-
- String html = getWidgetMarkup(widgetId).replaceFirst(controllerName, widget.getName() + "Ctrl");
-
- // new
- // String(map.get(WidgetConstant.WIDGET_MARKUP_LOCATION)).replaceFirst(functionName,
- // controllerName);;
-
- e = new ZipEntry(widget.getName() + "/markup/markup.html");
- out.putNextEntry(new ZipEntry(widget.getName() + "/markup/"));
- out.putNextEntry(e);
- data = html.getBytes();
+ byte[] data = javascript.getBytes();
out.write(data, 0, data.length);
out.closeEntry();
byte[] result = Files.readAllBytes(Paths.get(f.getPath()));
diff --git a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/WidgetCatalogServiceImpl.java b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/WidgetCatalogServiceImpl.java
index f5558e2e..76ca31a8 100644
--- a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/WidgetCatalogServiceImpl.java
+++ b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/service/impl/WidgetCatalogServiceImpl.java
@@ -148,9 +148,7 @@ public class WidgetCatalogServiceImpl implements WidgetCatalogService {
@Override
public WidgetCatalog getWidgetCatalog(Long widgetCatalogId) {
Session session = sessionFactory.getCurrentSession();
- Transaction tx = session.beginTransaction();
WidgetCatalog widget = (WidgetCatalog) session.get(WidgetCatalog.class, widgetCatalogId);
- tx.commit();
logger.debug("WidgetCatalogServiceImpl.getWidgetCatalog: getting widget={}", widget);
return widget;
}
diff --git a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java
index f20ed1b5..164a6983 100644
--- a/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java
+++ b/ecomp-portal-widget-ms/widget-ms/src/main/java/org/onap/portalapp/widget/utils/UnzipUtil.java
@@ -51,8 +51,7 @@ public class UnzipUtil {
ZipEntry entry = zipIn.getNextEntry();
Map<String, byte[]> map = new HashMap<>();
- String[] requiredKeys = { WidgetConstant.WIDGET_CONTROLLER_LOCATION, WidgetConstant.WIDGET_MARKUP_LOCATION,
- WidgetConstant.WIDGET_STYLE_LOCATION };
+ String[] requiredKeys = { WidgetConstant.WIDGET_CONTROLLER_LOCATION };
for (String k : requiredKeys)
map.put(k, null);