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
 
31
	private static final Logger LOGGER = LoggerFactory.getLogger(MongoMigrationUtil.class);
32
 
33
	public void migrate() throws ProfitMandiBusinessException{
34
		String fofoFormsJsonString = Mongo.getFofoFormsJsonString();
35
		Map<Integer, Map<String, Integer>> fofoIdPathMap = new HashMap<>();
36
 
37
		Gson gson = new Gson();
38
        Type paths = new TypeToken<List<Map<String, String>>>(){}.getType();
39
        List<Map<String, String>> maps = gson.fromJson(fofoFormsJsonString, paths);
40
        System.out.println(maps);
41
 
42
        for(Map<String, String> map : maps){
43
        	int id = 0;
44
        	Map<String, Integer> pathMap = new HashMap<>();
45
        	for(Map.Entry<String, String> entry : map.entrySet()){
46
        		if(entry.getKey().equals("_id")){
47
        			id = Integer.parseInt(entry.getValue());
48
        		}
49
        		if(entry.getValue().startsWith("/hsps-docs/")){
50
 
51
        			Document document = new Document();
52
 
53
        			try{
54
        				File file = new File(entry.getValue());
55
        				FileInputStream fileInputStream = new FileInputStream(file);
56
 
57
        				ContentType contentType = FileUtil.detectFileType(file);
58
        				document.setContentType(contentType);
59
        				document.setName(entry.getValue().substring(entry.getValue().lastIndexOf('/') + 1));
60
        				document.setPath(entry.getValue());
61
        				document.setPersisted(true);
62
        				document.setSize(file.length());
63
        				documentRepository.persist(document);
64
        				pathMap.put(entry.getKey(), document.getId());
65
        			}catch(FileNotFoundException fileNotFoundException){
66
 
67
        			}
68
        		}
69
        	}
70
        	if(!pathMap.isEmpty()){
71
        		fofoIdPathMap.put(id, pathMap);
72
        	}
73
        }
74
 
75
        LOGGER.info("fofoIdPathMap {}", fofoIdPathMap);
76
        for(Map.Entry<Integer, Map<String, Integer>> entry : fofoIdPathMap.entrySet()){
77
 
78
        	Mongo.updateColumnsById(entry.getValue(), entry.getKey());
79
        }
80
 
81
	}
82
}