| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.dao.util;
|
1 |
package com.spice.profitmandi.dao.util;
|
| 2 |
|
2 |
|
| 3 |
import java.io.File;
|
3 |
import java.io.File;
|
| 4 |
import java.io.FileInputStream;
|
- |
|
| 5 |
import java.io.FileNotFoundException;
|
- |
|
| 6 |
import java.io.IOException;
|
4 |
import java.io.IOException;
|
| 7 |
import java.lang.reflect.Type;
|
5 |
import java.lang.reflect.Type;
|
| - |
|
6 |
import java.time.LocalDateTime;
|
| 8 |
import java.util.HashMap;
|
7 |
import java.util.HashMap;
|
| - |
|
8 |
import java.util.HashSet;
|
| 9 |
import java.util.List;
|
9 |
import java.util.List;
|
| 10 |
import java.util.Map;
|
10 |
import java.util.Map;
|
| - |
|
11 |
import java.util.Set;
|
| 11 |
|
12 |
|
| 12 |
import org.apache.commons.io.FileUtils;
|
13 |
import org.apache.commons.io.FileUtils;
|
| 13 |
import org.slf4j.Logger;
|
14 |
import org.slf4j.Logger;
|
| 14 |
import org.slf4j.LoggerFactory;
|
15 |
import org.slf4j.LoggerFactory;
|
| 15 |
import org.springframework.beans.factory.annotation.Autowired;
|
16 |
import org.springframework.beans.factory.annotation.Autowired;
|
| Line 18... |
Line 19... |
| 18 |
import com.google.gson.Gson;
|
19 |
import com.google.gson.Gson;
|
| 19 |
import com.google.gson.reflect.TypeToken;
|
20 |
import com.google.gson.reflect.TypeToken;
|
| 20 |
import com.spice.profitmandi.common.enumuration.ContentType;
|
21 |
import com.spice.profitmandi.common.enumuration.ContentType;
|
| 21 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
22 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 22 |
import com.spice.profitmandi.common.util.FileUtil;
|
23 |
import com.spice.profitmandi.common.util.FileUtil;
|
| - |
|
24 |
import com.spice.profitmandi.common.util.Utils;
|
| 23 |
import com.spice.profitmandi.dao.entity.dtr.Document;
|
25 |
import com.spice.profitmandi.dao.entity.dtr.Document;
|
| - |
|
26 |
import com.spice.profitmandi.dao.entity.dtr.Retailer;
|
| - |
|
27 |
import com.spice.profitmandi.dao.entity.dtr.RetailerRegisteredAddress;
|
| - |
|
28 |
import com.spice.profitmandi.dao.entity.dtr.User;
|
| - |
|
29 |
import com.spice.profitmandi.dao.entity.dtr.UserAccounts;
|
| - |
|
30 |
import com.spice.profitmandi.dao.entity.dtr.UserRole;
|
| - |
|
31 |
import com.spice.profitmandi.dao.entity.user.Address;
|
| - |
|
32 |
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
|
| - |
|
33 |
import com.spice.profitmandi.dao.enumuration.dtr.RetailerType;
|
| - |
|
34 |
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
|
| 24 |
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
|
35 |
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
|
| 25 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
36 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
| - |
|
37 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
|
| - |
|
38 |
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
|
| - |
|
39 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
| - |
|
40 |
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
|
| - |
|
41 |
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
|
| - |
|
42 |
import com.spice.profitmandi.dao.repository.user.AddressRepository;
|
| 26 |
|
43 |
|
| 27 |
@Component
|
44 |
@Component
|
| 28 |
public class MongoMigrationUtil {
|
45 |
public class MigrationUtil {
|
| 29 |
|
46 |
|
| 30 |
@Autowired
|
47 |
@Autowired
|
| 31 |
DocumentRepository documentRepository;
|
48 |
DocumentRepository documentRepository;
|
| 32 |
|
49 |
|
| - |
|
50 |
@Autowired
|
| - |
|
51 |
UserRepository userRepository;
|
| - |
|
52 |
|
| - |
|
53 |
@Autowired
|
| - |
|
54 |
UserAccountRepository userAccountRepository;
|
| - |
|
55 |
|
| - |
|
56 |
@Autowired
|
| - |
|
57 |
RetailerRepository retailerRepository;
|
| - |
|
58 |
|
| - |
|
59 |
@Autowired
|
| - |
|
60 |
UserRoleRepository userRoleRepository;
|
| - |
|
61 |
|
| - |
|
62 |
@Autowired
|
| - |
|
63 |
AddressRepository addressRepository;
|
| - |
|
64 |
|
| - |
|
65 |
@Autowired
|
| - |
|
66 |
RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
|
| - |
|
67 |
|
| 33 |
|
68 |
|
| 34 |
@Autowired
|
69 |
@Autowired
|
| 35 |
Mongo mongoClient;
|
70 |
Mongo mongoClient;
|
| 36 |
|
71 |
|
| 37 |
|
72 |
|
| 38 |
private static final Logger LOGGER = LoggerFactory.getLogger(MongoMigrationUtil.class);
|
73 |
private static final Logger LOGGER = LoggerFactory.getLogger(MigrationUtil.class);
|
| 39 |
|
74 |
|
| 40 |
public void migrate() throws ProfitMandiBusinessException{
|
75 |
private List<Map<String, String>> getMongoFofoDoc(){
|
| 41 |
String fofoFormsJsonString = mongoClient.getFofoFormsJsonString();
|
76 |
String fofoFormsJsonString = mongoClient.getFofoFormsJsonString();
|
| 42 |
Map<Integer, Map<String, Integer>> fofoIdPathMap = new HashMap<>();
|
- |
|
| 43 |
|
77 |
|
| 44 |
Gson gson = new Gson();
|
78 |
Gson gson = new Gson();
|
| 45 |
Type paths = new TypeToken<List<Map<String, String>>>(){}.getType();
|
79 |
Type paths = new TypeToken<List<Map<String, String>>>(){}.getType();
|
| 46 |
List<Map<String, String>> maps = gson.fromJson(fofoFormsJsonString, paths);
|
80 |
List<Map<String, String>> maps = gson.fromJson(fofoFormsJsonString, paths);
|
| 47 |
System.out.println(maps);
|
81 |
return maps;
|
| - |
|
82 |
}
|
| - |
|
83 |
|
| - |
|
84 |
public void migrateMongoDocToDocumentId() throws ProfitMandiBusinessException{
|
| - |
|
85 |
List<Map<String, String>> maps = this.getMongoFofoDoc();
|
| - |
|
86 |
Map<Integer, Map<String, Integer>> fofoIdPathMap = new HashMap<>();
|
| 48 |
|
87 |
|
| 49 |
for(Map<String, String> map : maps){
|
88 |
for(Map<String, String> map : maps){
|
| 50 |
int id = 0;
|
89 |
int id = 0;
|
| 51 |
Map<String, Integer> pathMap = new HashMap<>();
|
90 |
Map<String, Integer> pathMap = new HashMap<>();
|
| 52 |
for(Map.Entry<String, String> entry : map.entrySet()){
|
91 |
for(Map.Entry<String, String> entry : map.entrySet()){
|
| Line 81... |
Line 120... |
| 81 |
}
|
120 |
}
|
| 82 |
}
|
121 |
}
|
| 83 |
|
122 |
|
| 84 |
LOGGER.info("fofoIdPathMap {}", fofoIdPathMap);
|
123 |
LOGGER.info("fofoIdPathMap {}", fofoIdPathMap);
|
| 85 |
for(Map.Entry<Integer, Map<String, Integer>> entry : fofoIdPathMap.entrySet()){
|
124 |
for(Map.Entry<Integer, Map<String, Integer>> entry : fofoIdPathMap.entrySet()){
|
| 86 |
|
- |
|
| 87 |
mongoClient.updateColumnsById(entry.getValue(), entry.getKey());
|
125 |
mongoClient.updateColumnsById(entry.getValue(), entry.getKey());
|
| 88 |
}
|
126 |
}
|
| 89 |
|
127 |
|
| 90 |
}
|
128 |
}
|
| - |
|
129 |
|
| - |
|
130 |
public void migrateUserToRetailer(){
|
| - |
|
131 |
Set<Integer> ids = new HashSet<>();
|
| - |
|
132 |
List<Integer> userIds = userRepository.selectIdAll();
|
| - |
|
133 |
List<UserAccounts> saholicUserAccounts = userAccountRepository.selectAllSaholicByUserIds(new HashSet<>(userIds));
|
| - |
|
134 |
for(UserAccounts saholicUserAccount : saholicUserAccounts){
|
| - |
|
135 |
Retailer retailer = new Retailer();
|
| - |
|
136 |
int retailerId = Integer.parseInt(saholicUserAccount.getAccount_key());
|
| - |
|
137 |
retailer.setId(retailerId);
|
| - |
|
138 |
retailer.setMigrated(true);
|
| - |
|
139 |
if(!ids.add(retailerId)){
|
| - |
|
140 |
LOGGER.info("Duplicate retailerId {}", retailerId);
|
| - |
|
141 |
}
|
| - |
|
142 |
try {
|
| - |
|
143 |
retailerRepository.persist(retailer);
|
| - |
|
144 |
} catch (ProfitMandiBusinessException e) {
|
| - |
|
145 |
LOGGER.info("Error occured while commiting retailer");
|
| - |
|
146 |
e.printStackTrace();
|
| - |
|
147 |
}
|
| - |
|
148 |
UserRole userRole1 = new UserRole();
|
| - |
|
149 |
userRole1.setRoleType(RoleType.RETAILER);
|
| - |
|
150 |
userRole1.setUserId(saholicUserAccount.getUser_id());
|
| - |
|
151 |
try {
|
| - |
|
152 |
userRoleRepository.persist(userRole1);
|
| - |
|
153 |
} catch (ProfitMandiBusinessException e) {
|
| - |
|
154 |
e.printStackTrace();
|
| - |
|
155 |
}
|
| - |
|
156 |
|
| - |
|
157 |
UserRole userRole2 = new UserRole();
|
| - |
|
158 |
userRole2.setRoleType(RoleType.USER);
|
| - |
|
159 |
userRole2.setUserId(saholicUserAccount.getUser_id());
|
| - |
|
160 |
try {
|
| - |
|
161 |
userRoleRepository.persist(userRole2);
|
| - |
|
162 |
} catch (ProfitMandiBusinessException e) {
|
| - |
|
163 |
e.printStackTrace();
|
| - |
|
164 |
}
|
| - |
|
165 |
}
|
| - |
|
166 |
}
|
| - |
|
167 |
|
| - |
|
168 |
public void migrateMongoDocToRetailer() throws ProfitMandiBusinessException{
|
| - |
|
169 |
List<Map<String, String>> maps = this.getMongoFofoDoc();
|
| - |
|
170 |
Set<String> emailIds = new HashSet<>();
|
| - |
|
171 |
for(Map<String, String> map : maps){
|
| - |
|
172 |
if(map.containsKey("registeredEmail1")){
|
| - |
|
173 |
emailIds.add(map.get("registeredEmail1"));
|
| - |
|
174 |
}
|
| - |
|
175 |
}
|
| - |
|
176 |
List<String> foundEmailIds = userRepository.selectEmailIdsByEmailIds(emailIds);
|
| - |
|
177 |
emailIds.removeAll(foundEmailIds);
|
| - |
|
178 |
for(Map<String, String> map : maps){
|
| - |
|
179 |
if(map.containsKey("registeredEmail1") && emailIds.contains(map.get("registeredEmail1"))){
|
| - |
|
180 |
User user = new User();
|
| - |
|
181 |
user.setFirstName("");
|
| - |
|
182 |
user.setLastName("");
|
| - |
|
183 |
user.setCity(map.containsKey("city") ? map.get("city") : "");
|
| - |
|
184 |
user.setPinCode(Integer.valueOf(map.containsKey("pincode") ? map.get("pincode") : ""));
|
| - |
|
185 |
user.setMobileNumber(map.containsKey("mobile") ? map.get("mobile") : "");
|
| - |
|
186 |
user.setEmailId(map.get("registeredEmail1"));
|
| - |
|
187 |
user.setUsername(map.get("registeredEmail1"));
|
| - |
|
188 |
user.setPassword("");
|
| - |
|
189 |
user.setMobile_verified(false);
|
| - |
|
190 |
user.setReferral_url("");
|
| - |
|
191 |
user.setGroup_id(1);
|
| - |
|
192 |
user.setStatus(0);
|
| - |
|
193 |
user.setActivated(false);
|
| - |
|
194 |
user.setCreateTimestamp(LocalDateTime.now());
|
| - |
|
195 |
user.setUpdateTimestamp(LocalDateTime.now());
|
| - |
|
196 |
userRepository.persist(user);
|
| - |
|
197 |
|
| - |
|
198 |
in.shop2020.model.v1.user.User saholicUser = Utils.createSaholicUser(map.get("registeredEmail1"));
|
| - |
|
199 |
|
| - |
|
200 |
Retailer retailer = new Retailer();
|
| - |
|
201 |
retailer.setActive(true);
|
| - |
|
202 |
retailer.setFofo(true);
|
| - |
|
203 |
retailer.setId(Long.valueOf(saholicUser.getUserId()).intValue());
|
| - |
|
204 |
retailer.setMigrated(true);
|
| - |
|
205 |
retailer.setName(map.containsKey("registeredBusinessName") ? map.get("registeredBusinessName") : "");
|
| - |
|
206 |
retailer.setType(RetailerType.GSTIN);
|
| - |
|
207 |
retailerRepository.persist(retailer);
|
| - |
|
208 |
|
| - |
|
209 |
Address retailerAddress = new Address();
|
| - |
|
210 |
retailerAddress.setCity(map.containsKey("city") ? map.get("city") : "");
|
| - |
|
211 |
retailerAddress.setCountry("India");
|
| - |
|
212 |
retailerAddress.setEnabled(true);
|
| - |
|
213 |
retailerAddress.setLandmark("");
|
| - |
|
214 |
retailerAddress.setLine1(map.containsKey("line1") ? map.get("line1") : "");
|
| - |
|
215 |
retailerAddress.setLine2(map.containsKey("line2") ? map.get("line2") : "");
|
| - |
|
216 |
retailerAddress.setName(map.containsKey("registeredBusinessName") ? map.get("registeredBusinessName") : "");
|
| - |
|
217 |
retailerAddress.setPhoneNumber(map.containsKey("mobile") ? map.get("mobile") : "");
|
| - |
|
218 |
retailerAddress.setPinCode(map.containsKey("pincode") ? map.get("pincode") : "");
|
| - |
|
219 |
retailerAddress.setState(map.containsKey("state") ? map.get("state") : "");
|
| - |
|
220 |
addressRepository.persist(retailerAddress);
|
| - |
|
221 |
|
| - |
|
222 |
RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
|
| - |
|
223 |
retailerRegisteredAddress.setAddressId(retailerAddress.getId());
|
| - |
|
224 |
retailerRegisteredAddress.setRetailerId(retailer.getId());
|
| - |
|
225 |
retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
|
| - |
|
226 |
|
| - |
|
227 |
UserAccounts saholicUserAccounts = new UserAccounts();
|
| - |
|
228 |
saholicUserAccounts.setAccount_key(String.valueOf(saholicUser.getUserId()));
|
| - |
|
229 |
saholicUserAccounts.setAccount_type(AccountType.saholic);
|
| - |
|
230 |
saholicUserAccounts.setUser_id(user.getId());
|
| - |
|
231 |
userAccountRepository.persist(saholicUserAccounts);
|
| - |
|
232 |
UserAccounts cartUserAccounts = new UserAccounts();
|
| - |
|
233 |
cartUserAccounts.setAccount_key(String.valueOf(saholicUser.getActiveCartId()));
|
| - |
|
234 |
cartUserAccounts.setAccount_type(AccountType.cartId);
|
| - |
|
235 |
cartUserAccounts.setUser_id(user.getId());
|
| - |
|
236 |
userAccountRepository.persist(cartUserAccounts);
|
| - |
|
237 |
|
| - |
|
238 |
}
|
| - |
|
239 |
}
|
| - |
|
240 |
}
|
| - |
|
241 |
|
| - |
|
242 |
public void migratePrivateDealToRetailer(){
|
| - |
|
243 |
|
| - |
|
244 |
}
|
| 91 |
}
|
245 |
}
|