+
+ /**
+ * 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);
+ }
+
+
+
+