Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
22125 ashik.ali 1
package com.spice.profitmandi.dao.util;
2
 
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
22178 amit.gupta 6
import java.io.IOException;
22125 ashik.ali 7
import java.lang.reflect.Type;
8
import java.util.HashMap;
9
import java.util.List;
10
import java.util.Map;
11
 
22178 amit.gupta 12
import org.apache.commons.io.FileUtils;
22125 ashik.ali 13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.stereotype.Component;
17
 
18
import com.google.gson.Gson;
19
import com.google.gson.reflect.TypeToken;
20
import com.spice.profitmandi.common.enumuration.ContentType;
21
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
22
import com.spice.profitmandi.common.util.FileUtil;
23
import com.spice.profitmandi.dao.entity.dtr.Document;
24
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
25
import com.spice.profitmandi.dao.repository.dtr.Mongo;
26
 
27
@Component
28
public class MongoMigrationUtil {
29
 
30
	@Autowired
31
	DocumentRepository documentRepository;
32
 
22162 amit.gupta 33
 
34
	@Autowired
35
	Mongo mongoClient;
36
 
37
 
22125 ashik.ali 38
	private static final Logger LOGGER = LoggerFactory.getLogger(MongoMigrationUtil.class);
39
 
40
	public void migrate() throws ProfitMandiBusinessException{
22162 amit.gupta 41
		String fofoFormsJsonString = mongoClient.getFofoFormsJsonString();
22125 ashik.ali 42
		Map<Integer, Map<String, Integer>> fofoIdPathMap = new HashMap<>();
43
 
44
		Gson gson = new Gson();
45
        Type paths = new TypeToken<List<Map<String, String>>>(){}.getType();
46
        List<Map<String, String>> maps = gson.fromJson(fofoFormsJsonString, paths);
47
        System.out.println(maps);
48
 
49
        for(Map<String, String> map : maps){
50
        	int id = 0;
51
        	Map<String, Integer> pathMap = new HashMap<>();
52
        	for(Map.Entry<String, String> entry : map.entrySet()){
53
        		if(entry.getKey().equals("_id")){
54
        			id = Integer.parseInt(entry.getValue());
55
        		}
56
        		if(entry.getValue().startsWith("/hsps-docs/")){
57
 
58
        			Document document = new Document();
59
 
60
        			try{
22178 amit.gupta 61
        				File srcFile = new File(entry.getValue());
62
        				String destFileName = entry.getValue().substring(entry.getValue().lastIndexOf('/') + 1);
63
        				String destFilePath = "/uploads/"; 
64
        				File destFile = new File(destFileName + destFilePath);
65
        				FileUtils.copyFile(srcFile, destFile);
66
        				ContentType contentType = FileUtil.detectFileType(srcFile);
22125 ashik.ali 67
        				document.setContentType(contentType);
22178 amit.gupta 68
        				document.setName(destFileName);
69
        				document.setPath(destFilePath);
22125 ashik.ali 70
        				document.setPersisted(true);
22178 amit.gupta 71
        				document.setSize(srcFile.length());
22125 ashik.ali 72
        				documentRepository.persist(document);
73
        				pathMap.put(entry.getKey(), document.getId());
22178 amit.gupta 74
        			}catch(IOException ioException) {
75
        				LOGGER.info("IOException occurred");
22125 ashik.ali 76
        			}
77
        		}
78
        	}
79
        	if(!pathMap.isEmpty()){
80
        		fofoIdPathMap.put(id, pathMap);
81
        	}
82
        }
83
 
84
        LOGGER.info("fofoIdPathMap {}", fofoIdPathMap);
85
        for(Map.Entry<Integer, Map<String, Integer>> entry : fofoIdPathMap.entrySet()){
86
 
22162 amit.gupta 87
        	mongoClient.updateColumnsById(entry.getValue(), entry.getKey());
22125 ashik.ali 88
        }
89
 
90
	}
91
}