Rev 22178 | Rev 22182 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.dao.util;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.lang.reflect.Type;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.commons.io.FileUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import com.google.gson.Gson;import com.google.gson.reflect.TypeToken;import com.spice.profitmandi.common.enumuration.ContentType;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.util.FileUtil;import com.spice.profitmandi.dao.entity.dtr.Document;import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;import com.spice.profitmandi.dao.repository.dtr.Mongo;@Componentpublic class MongoMigrationUtil {@AutowiredDocumentRepository documentRepository;@AutowiredMongo mongoClient;private static final Logger LOGGER = LoggerFactory.getLogger(MongoMigrationUtil.class);public void migrate() throws ProfitMandiBusinessException{String fofoFormsJsonString = mongoClient.getFofoFormsJsonString();Map<Integer, Map<String, Integer>> fofoIdPathMap = new HashMap<>();Gson gson = new Gson();Type paths = new TypeToken<List<Map<String, String>>>(){}.getType();List<Map<String, String>> maps = gson.fromJson(fofoFormsJsonString, paths);System.out.println(maps);for(Map<String, String> map : maps){int id = 0;Map<String, Integer> pathMap = new HashMap<>();for(Map.Entry<String, String> entry : map.entrySet()){if(entry.getKey().equals("_id")){id = (int)Double.parseDouble(entry.getValue());}if(entry.getValue().startsWith("/hsps-docs/")){Document document = new Document();try{File srcFile = new File(entry.getValue());String destFileName = entry.getValue().substring(entry.getValue().lastIndexOf('/') + 1);String destFilePath = "/uploads/";File destFile = new File(destFileName + destFilePath);FileUtils.copyFile(srcFile, destFile);ContentType contentType = FileUtil.detectFileType(srcFile);document.setContentType(contentType);document.setName(destFileName);document.setPath(destFilePath);document.setPersisted(true);document.setSize(srcFile.length());documentRepository.persist(document);pathMap.put(entry.getKey(), document.getId());}catch(IOException ioException) {LOGGER.info("IOException occurred");}}}if(!pathMap.isEmpty()){fofoIdPathMap.put(id, pathMap);}}LOGGER.info("fofoIdPathMap {}", fofoIdPathMap);for(Map.Entry<Integer, Map<String, Integer>> entry : fofoIdPathMap.entrySet()){mongoClient.updateColumnsById(entry.getValue(), entry.getKey());}}}