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