Subversion Repositories SmartDukaan

Rev

Rev 37102 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
22125 ashik.ali 1
package com.spice.profitmandi.dao.util;
2
 
3
import com.google.gson.Gson;
4
import com.google.gson.reflect.TypeToken;
5
import com.spice.profitmandi.common.enumuration.ContentType;
6
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
22476 ashik.ali 7
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22125 ashik.ali 8
import com.spice.profitmandi.common.util.FileUtil;
22352 ashik.ali 9
import com.spice.profitmandi.dao.entity.dtr.User;
32482 amit.gupta 10
import com.spice.profitmandi.dao.entity.dtr.*;
23365 ashik.ali 11
import com.spice.profitmandi.dao.entity.fofo.FofoPartnerPaymentOption;
12
import com.spice.profitmandi.dao.entity.fofo.PaymentOption;
32482 amit.gupta 13
import com.spice.profitmandi.dao.entity.user.*;
22352 ashik.ali 14
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
15
import com.spice.profitmandi.dao.enumuration.dtr.RetailerType;
16
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
23365 ashik.ali 17
import com.spice.profitmandi.dao.enumuration.fofo.PaymentOptionType;
22352 ashik.ali 18
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
32482 amit.gupta 19
import com.spice.profitmandi.dao.repository.dtr.*;
23365 ashik.ali 20
import com.spice.profitmandi.dao.repository.fofo.FofoPartnerPaymentOptionRepository;
21
import com.spice.profitmandi.dao.repository.fofo.PaymentOptionRepository;
32482 amit.gupta 22
import com.spice.profitmandi.dao.repository.user.*;
22471 ashik.ali 23
import in.shop2020.model.v1.user.CartStatus;
32482 amit.gupta 24
import org.apache.commons.io.FileUtils;
25
import org.apache.logging.log4j.LogManager;
26
import org.apache.logging.log4j.Logger;
27
import org.springframework.beans.factory.annotation.Autowired;
28
import org.springframework.stereotype.Component;
22471 ashik.ali 29
 
32482 amit.gupta 30
import java.io.File;
31
import java.io.IOException;
32
import java.lang.reflect.Type;
33
import java.time.LocalDateTime;
34
import java.util.*;
35
 
22125 ashik.ali 36
@Component
32482 amit.gupta 37
public class
38
MigrationUtil {
22125 ashik.ali 39
 
40
	@Autowired
22868 ashik.ali 41
	private DocumentRepository documentRepository;
22125 ashik.ali 42
 
22352 ashik.ali 43
	@Autowired
22868 ashik.ali 44
	private UserRepository userRepository;
22352 ashik.ali 45
 
46
	@Autowired
22868 ashik.ali 47
	private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
22471 ashik.ali 48
 
49
	@Autowired
22868 ashik.ali 50
	private CartRepository cartRepository;
22471 ashik.ali 51
 
52
	@Autowired
22868 ashik.ali 53
	private UserAccountRepository userAccountRepository;
22352 ashik.ali 54
 
55
	@Autowired
22868 ashik.ali 56
	private RetailerRepository retailerRepository;
22352 ashik.ali 57
 
58
	@Autowired
22868 ashik.ali 59
	private UserRoleRepository userRoleRepository;
22352 ashik.ali 60
 
61
	@Autowired
22868 ashik.ali 62
	private AddressRepository addressRepository;
22352 ashik.ali 63
 
64
	@Autowired
22868 ashik.ali 65
	private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
22352 ashik.ali 66
 
22471 ashik.ali 67
	@Autowired
23066 ashik.ali 68
	private ShopRepository shopRepository;
69
 
70
	@Autowired
71
	private ShopAddressRepository shopAddressRepository;
72
 
73
	@Autowired
23365 ashik.ali 74
	private FofoPartnerPaymentOptionRepository fofoPartnerPaymentOptionRepository;
75
 
76
	@Autowired
77
	private PaymentOptionRepository paymentOptionRepository;
78
 
79
	@Autowired
23781 ashik.ali 80
	private RoleRepository roleRepository;
81
 
82
	@Autowired
22868 ashik.ali 83
	private Mongo mongoClient;
22162 amit.gupta 84
 
85
 
23568 govind 86
	private static final Logger LOGGER = LogManager.getLogger(MigrationUtil.class);
22125 ashik.ali 87
 
22352 ashik.ali 88
	private List<Map<String, String>> getMongoFofoDoc(){
22162 amit.gupta 89
		String fofoFormsJsonString = mongoClient.getFofoFormsJsonString();
22471 ashik.ali 90
		LOGGER.info("mongoFofoDoc {}", fofoFormsJsonString);
22125 ashik.ali 91
		Gson gson = new Gson();
92
        Type paths = new TypeToken<List<Map<String, String>>>(){}.getType();
93
        List<Map<String, String>> maps = gson.fromJson(fofoFormsJsonString, paths);
22352 ashik.ali 94
        return maps;
95
	}
96
 
97
	public void migrateMongoDocToDocumentId() throws ProfitMandiBusinessException{
98
		List<Map<String, String>> maps = this.getMongoFofoDoc();
99
        Map<Integer, Map<String, Integer>> fofoIdPathMap = new HashMap<>();
22125 ashik.ali 100
 
101
        for(Map<String, String> map : maps){
102
        	int id = 0;
103
        	Map<String, Integer> pathMap = new HashMap<>();
104
        	for(Map.Entry<String, String> entry : map.entrySet()){
105
        		if(entry.getKey().equals("_id")){
22181 amit.gupta 106
        			id = (int)Double.parseDouble(entry.getValue());
22125 ashik.ali 107
        		}
108
        		if(entry.getValue().startsWith("/hsps-docs/")){
109
 
110
        			Document document = new Document();
111
 
112
        			try{
22178 amit.gupta 113
        				File srcFile = new File(entry.getValue());
114
        				String destFileName = entry.getValue().substring(entry.getValue().lastIndexOf('/') + 1);
115
        				String destFilePath = "/uploads/"; 
22182 amit.gupta 116
        				File destFile = new File(destFilePath + destFileName);
22178 amit.gupta 117
        				FileUtils.copyFile(srcFile, destFile);
118
        				ContentType contentType = FileUtil.detectFileType(srcFile);
22125 ashik.ali 119
        				document.setContentType(contentType);
22178 amit.gupta 120
        				document.setName(destFileName);
121
        				document.setPath(destFilePath);
22125 ashik.ali 122
        				document.setPersisted(true);
22178 amit.gupta 123
        				document.setSize(srcFile.length());
22125 ashik.ali 124
        				documentRepository.persist(document);
125
        				pathMap.put(entry.getKey(), document.getId());
22178 amit.gupta 126
        			}catch(IOException ioException) {
127
        				LOGGER.info("IOException occurred");
22125 ashik.ali 128
        			}
129
        		}
130
        	}
131
        	if(!pathMap.isEmpty()){
132
        		fofoIdPathMap.put(id, pathMap);
133
        	}
134
        }
135
 
136
        LOGGER.info("fofoIdPathMap {}", fofoIdPathMap);
137
        for(Map.Entry<Integer, Map<String, Integer>> entry : fofoIdPathMap.entrySet()){
22162 amit.gupta 138
        	mongoClient.updateColumnsById(entry.getValue(), entry.getKey());
22125 ashik.ali 139
        }
140
 
141
	}
22352 ashik.ali 142
 
23269 ashik.ali 143
	private Set<Integer> toRetailerIds(List<UserAccount> userAccounts){
22868 ashik.ali 144
		Set<Integer> retailerIds = new HashSet<>();
23269 ashik.ali 145
		for(UserAccount saholicUserAccount : userAccounts){
146
			int retailerId = saholicUserAccount.getAccountKey();
22868 ashik.ali 147
			retailerIds.add(retailerId);
148
		}
149
		return retailerIds;
150
	}
151
 
23269 ashik.ali 152
	private Set<Integer> toUserIds(List<UserAccount> userAccounts, Set<Integer> retailerIds){
22868 ashik.ali 153
		Set<Integer> userIds = new HashSet<>();
23269 ashik.ali 154
		for(UserAccount saholicUserAccount : userAccounts){
155
			int retailerId = saholicUserAccount.getAccountKey();
22868 ashik.ali 156
			if(retailerIds.contains(retailerId)){
23269 ashik.ali 157
				userIds.add(saholicUserAccount.getUserId());
22416 amit.gupta 158
			}
22868 ashik.ali 159
		}
160
		return userIds;
161
	}
162
 
23781 ashik.ali 163
	public void migrateUserToRetailer() throws ProfitMandiBusinessException{
22868 ashik.ali 164
 
23269 ashik.ali 165
		List<UserAccount> saholicUserAccounts = userAccountRepository.selectAllSaholicByUserIds(new HashSet<>(userRepository.selectIdAll()));
22868 ashik.ali 166
		if(saholicUserAccounts.isEmpty()){
167
			return;
168
		}
169
		Set<Integer> retailerIds = this.toRetailerIds(saholicUserAccounts);
170
		List<Integer> foundRetailerIds = retailerRepository.selectIdsByIds(retailerIds);
171
		retailerIds.removeAll(foundRetailerIds);
172
		for(int retailerId : retailerIds){
22416 amit.gupta 173
			Retailer retailer = new Retailer();
22352 ashik.ali 174
			try {
22416 amit.gupta 175
				retailer.setId(retailerId);
176
				retailer.setMigrated(true);
22352 ashik.ali 177
				retailerRepository.persist(retailer);
22868 ashik.ali 178
				LOGGER.info("Retailer migrated {}", retailerId);
22352 ashik.ali 179
			} catch (ProfitMandiBusinessException e) {
180
				LOGGER.info("Error occured while commiting retailer");
181
				e.printStackTrace();
182
			}
22868 ashik.ali 183
		}
184
 
185
		Set<Integer> userIds = this.toUserIds(saholicUserAccounts, retailerIds);
186
 
22886 ashik.ali 187
		if(userIds.isEmpty()){
188
			return;
189
		}
22868 ashik.ali 190
 
23781 ashik.ali 191
		Role roleRetailer = roleRepository.selectByName(RoleType.RETAILER.toString());
192
 
193
		List<Integer> foundRetailerRoleUserIds = userRoleRepository.selectUserIdsByUserIdsAndRoleId(userIds, roleRetailer.getId());
194
 
22868 ashik.ali 195
		Set<Integer> notFoundRetailerRoleUserIds = new HashSet<>(userIds);
196
		notFoundRetailerRoleUserIds.removeAll(foundRetailerRoleUserIds);
197
 
23781 ashik.ali 198
 
199
 
22868 ashik.ali 200
		for(int userId : notFoundRetailerRoleUserIds){
201
			UserRole retailerRole = new UserRole();
23781 ashik.ali 202
			retailerRole.setRoleId(roleRetailer.getId());
22868 ashik.ali 203
			retailerRole.setUserId(userId);
204
			try {
205
				userRoleRepository.persist(retailerRole);
206
				LOGGER.info("UserId {} with Retailer Role migrated", userId);
207
			} catch (ProfitMandiBusinessException e) {
208
				e.printStackTrace();
22352 ashik.ali 209
			}
22868 ashik.ali 210
		}
211
 
23781 ashik.ali 212
		Role roleUser = roleRepository.selectByName(RoleType.USER.toString());
22868 ashik.ali 213
 
23781 ashik.ali 214
		List<Integer> foundUserRoleUserIds = userRoleRepository.selectUserIdsByUserIdsAndRoleId(userIds, roleUser.getId());
215
 
22868 ashik.ali 216
		Set<Integer> notFoundUserRoleUserIds = new HashSet<>(userIds);
217
		notFoundUserRoleUserIds.removeAll(foundUserRoleUserIds);
218
 
23781 ashik.ali 219
 
22868 ashik.ali 220
		for(int userId : notFoundUserRoleUserIds){
221
			UserRole userRole = new UserRole();
23781 ashik.ali 222
			userRole.setRoleId(roleUser.getId());
22868 ashik.ali 223
			userRole.setUserId(userId);
224
			try {
225
				userRoleRepository.persist(userRole);
226
				LOGGER.info("UserId {} with User Role migrated", userId);
227
			} catch (ProfitMandiBusinessException e) {
228
				e.printStackTrace();
22352 ashik.ali 229
			}
230
		}
22868 ashik.ali 231
 
22352 ashik.ali 232
	}
233
 
234
	public void migrateMongoDocToRetailer() throws ProfitMandiBusinessException{
235
		List<Map<String, String>> maps = this.getMongoFofoDoc();
22471 ashik.ali 236
		//LOGGER.info("mongoFofoDocs {}", maps);
22352 ashik.ali 237
		for(Map<String, String> map : maps){
238
			if(map.containsKey("registeredEmail1")){
239
 
22476 ashik.ali 240
				User user = null;
241
 
242
				try{
243
					user = userRepository.selectByEmailId(map.get("registeredEmail1"));
244
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
23202 ashik.ali 245
				}
246
				if(user == null){
23329 ashik.ali 247
					user = userRepository.selectBySecondryEmailId(map.get("registeredEmail1"));
248
				}
249
				if(user == null){
22476 ashik.ali 250
					user = new User();
251
					user.setFirstName("");
252
					user.setLastName("");
253
					user.setCity(map.containsKey("city") ? map.get("city") : "");
254
					user.setPinCode(Integer.valueOf(map.containsKey("pincode") ? map.get("pincode") : ""));
255
					user.setMobileNumber(map.containsKey("mobile") ? map.get("mobile") : "");
256
					user.setEmailId(map.get("registeredEmail1"));
257
					user.setUsername(map.get("registeredEmail1"));
258
					user.setPassword("");
259
					user.setMobile_verified(false);
260
					user.setReferral_url("");
261
					user.setGroup_id(1);
22504 amit.gupta 262
					user.setStatus(1);
23300 amit.gupta 263
					user.setActivated(true);
22476 ashik.ali 264
					user.setCreateTimestamp(LocalDateTime.now());
265
					user.setUpdateTimestamp(LocalDateTime.now());
266
					userRepository.persist(user);
267
				}
268
 
269
 
22471 ashik.ali 270
				//in.shop2020.model.v1.user.User saholicUser = Utils.createSaholicUser(map.get("registeredEmail1"));
22352 ashik.ali 271
 
22471 ashik.ali 272
				com.spice.profitmandi.dao.entity.user.User saholicUser = null;
273
				boolean foundRetailer = false;
23067 ashik.ali 274
				try{
275
					saholicUser = userUserRepository.selectByEmailId(user.getEmailId());
276
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
277
 
278
				}
22471 ashik.ali 279
				if(saholicUser == null){
280
					Cart cart = new Cart();
281
					cart.setCartStatus(CartStatus.ACTIVE);
282
					cartRepository.persist(cart);
283
					saholicUser = new com.spice.profitmandi.dao.entity.user.User();
284
					saholicUser.setEmailId(user.getEmailId());
285
					saholicUser.setName(map.containsKey("registeredBusinessName") ? map.get("registeredBusinessName") : "");
286
					saholicUser.setActiveCartId(cart.getId());
23300 amit.gupta 287
					saholicUser.setCreateTimestamp(LocalDateTime.now());
22471 ashik.ali 288
					userUserRepository.persist(saholicUser);
289
				}else{
290
					foundRetailer = true;
291
				}
292
 
22476 ashik.ali 293
				Retailer retailer = null;
22471 ashik.ali 294
 
22476 ashik.ali 295
				try{
296
					retailer = retailerRepository.selectById(saholicUser.getId());
297
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
298
					retailer = new Retailer();
299
				}
23443 amit.gupta 300
				retailer.setActive(true);
301
				retailer.setId(saholicUser.getId());
302
				retailer.setMigrated(true);
303
				retailer.setName(map.containsKey("registeredBusinessName") ? map.get("registeredBusinessName") : "");
304
				retailer.setType(RetailerType.GSTIN);
305
				retailerRepository.persist(retailer);
22352 ashik.ali 306
 
22476 ashik.ali 307
 
22352 ashik.ali 308
				Address retailerAddress = new Address();
309
 
23880 ashik.ali 310
				List<Address> retailerAddresses = addressRepository.selectAllByRetailerId(retailer.getId(), 1, 100);
22476 ashik.ali 311
				if(retailerAddresses.isEmpty()){
312
					retailerAddress.setCity(map.containsKey(ProfitMandiConstants.CITY) ? map.get(ProfitMandiConstants.CITY) : "");
313
					retailerAddress.setCountry("India");
314
					retailerAddress.setEnabled(true);
315
					retailerAddress.setLandmark("");
316
					retailerAddress.setLine1(map.containsKey(ProfitMandiConstants.LINE1) ? map.get(ProfitMandiConstants.LINE1) : "");
317
					retailerAddress.setLine2(map.containsKey(ProfitMandiConstants.LINE2) ? map.get(ProfitMandiConstants.LINE2) : "");
318
					retailerAddress.setName(map.containsKey("registeredBusinessName") ? map.get("registeredBusinessName") : "");
319
					retailerAddress.setPhoneNumber(map.containsKey("mobile") ? map.get("mobile") : "");
320
					retailerAddress.setPinCode(map.containsKey("pincode") ? map.get("pincode") : "");
321
					retailerAddress.setState(map.containsKey(ProfitMandiConstants.STATE) ? map.get(ProfitMandiConstants.STATE) : "");
322
					retailerAddress.setRetaierId(retailer.getId());
323
					addressRepository.persist(retailerAddress);
324
				}
22352 ashik.ali 325
 
326
 
22476 ashik.ali 327
				RetailerRegisteredAddress retailerRegisteredAddress = null;
328
				try{
329
					retailerRegisteredAddress = retailerRegisteredAddressRepository.selectByAddressIdAndRetailerId(retailerAddress.getId(), retailer.getId());
23075 ashik.ali 330
					//retailerRegisteredAddress = retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailer.getId());
22476 ashik.ali 331
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
332
					retailerRegisteredAddress = new RetailerRegisteredAddress();
333
					retailerRegisteredAddress.setAddressId(retailerAddress.getId());
334
					retailerRegisteredAddress.setRetailerId(retailer.getId());
335
					retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
336
				}
22471 ashik.ali 337
 
22476 ashik.ali 338
 
23066 ashik.ali 339
				List<Shop> shops = shopRepository.selectByRetailerId(retailer.getId());
340
				if(shops.isEmpty()){
341
					Shop shop = new Shop();
342
					shop.setName(retailer.getName());
343
					shop.setAddressId(retailerAddress.getId());
344
					shop.setDocumentId(retailer.getDocumentId());
345
					shop.setRetailerId(retailer.getId());
346
					shopRepository.persist(shop);
347
					ShopAddress shopAddress = new ShopAddress();
348
					shopAddress.setAddressId(retailerAddress.getId());
349
					shopAddress.setShopId(shop.getId());
350
					shopAddressRepository.persist(shopAddress);
351
				}
352
 
23269 ashik.ali 353
				UserAccount saholicUserAccounts = null;
22476 ashik.ali 354
				try{
355
					saholicUserAccounts = userAccountRepository.selectByUserIdRetailerIdAccountType(user.getId(), retailer.getId(), AccountType.saholic);
356
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
23269 ashik.ali 357
					saholicUserAccounts = new UserAccount();
358
					saholicUserAccounts.setAccountKey(saholicUser.getId());
359
					saholicUserAccounts.setType(AccountType.saholic);
360
					saholicUserAccounts.setUserId(user.getId());
22476 ashik.ali 361
					userAccountRepository.persist(saholicUserAccounts);
23269 ashik.ali 362
					UserAccount cartUserAccounts = new UserAccount();
363
					cartUserAccounts.setAccountKey(saholicUser.getActiveCartId());
364
					cartUserAccounts.setType(AccountType.cartId);
365
					cartUserAccounts.setUserId(user.getId());
22476 ashik.ali 366
					userAccountRepository.persist(cartUserAccounts);
367
				}
368
 
23781 ashik.ali 369
				Role roleUser = roleRepository.selectByName(RoleType.USER.toString());
22476 ashik.ali 370
 
371
				UserRole userRole = null;
23781 ashik.ali 372
 
22476 ashik.ali 373
				try{
23781 ashik.ali 374
					userRole = userRoleRepository.selectByUserIdAndRoleId(user.getId(), roleUser.getId());
22476 ashik.ali 375
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
23781 ashik.ali 376
					Role role = roleRepository.selectByName(RoleType.USER.toString());
22476 ashik.ali 377
					userRole = new UserRole();
23781 ashik.ali 378
					userRole.setRoleId(role.getId());
22476 ashik.ali 379
					userRole.setUserId(user.getId());
380
					userRoleRepository.persist(userRole);
381
				}
382
 
23781 ashik.ali 383
				Role roleRetailer = roleRepository.selectByName(RoleType.RETAILER.toString());
384
 
22476 ashik.ali 385
				UserRole retailerRole = null;
386
				try{
23781 ashik.ali 387
					retailerRole = userRoleRepository.selectByUserIdAndRoleId(user.getId(), roleRetailer.getId());
22476 ashik.ali 388
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
23781 ashik.ali 389
					Role role = roleRepository.selectByName(RoleType.RETAILER.toString());
22476 ashik.ali 390
					retailerRole = new UserRole();
23781 ashik.ali 391
					retailerRole.setRoleId(role.getId());
22476 ashik.ali 392
					retailerRole.setUserId(user.getId());
393
					userRoleRepository.persist(retailerRole);
394
				}
395
 
23781 ashik.ali 396
				Role roleFofo = roleRepository.selectByName(RoleType.FOFO.toString());
397
 
22476 ashik.ali 398
				UserRole fofoRole = null;
399
				try{
23781 ashik.ali 400
					fofoRole = userRoleRepository.selectByUserIdAndRoleId(user.getId(), roleFofo.getId());
22476 ashik.ali 401
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
23781 ashik.ali 402
					Role role = roleRepository.selectByName(RoleType.FOFO.toString());
22476 ashik.ali 403
					fofoRole = new UserRole();
23781 ashik.ali 404
					fofoRole.setRoleId(role.getId());
22476 ashik.ali 405
					fofoRole.setUserId(user.getId());
406
					userRoleRepository.persist(fofoRole);
407
				}
408
 
23365 ashik.ali 409
				List<Integer> paymentOptionIds = fofoPartnerPaymentOptionRepository.selectPaymentOptionIdsByFofoId(retailer.getId());
410
				if(paymentOptionIds.isEmpty()){
411
					PaymentOption paymentOption = null;
412
					try{
413
						paymentOption = paymentOptionRepository.selectByName(PaymentOptionType.CASH.toString());
414
					}catch(ProfitMandiBusinessException profitMandiBusinessException){
415
						paymentOption = new PaymentOption();
416
						paymentOption.setName(PaymentOptionType.CASH.toString());
417
						paymentOptionRepository.persist(paymentOption);
418
					}
419
					FofoPartnerPaymentOption fofoPartnerPaymentOption = new FofoPartnerPaymentOption();
420
					fofoPartnerPaymentOption.setFofoId(retailer.getId());
421
					fofoPartnerPaymentOption.setPaymentOptionId(paymentOption.getId());
422
					fofoPartnerPaymentOptionRepository.persist(fofoPartnerPaymentOption);
423
				}
424
 
37648 amit 425
				if(!foundRetailer){
22471 ashik.ali 426
					LOGGER.info("retailer not found");
427
					saholicUser.setAddressId(retailerAddress.getId());
428
					userUserRepository.persist(saholicUser);
429
				}
22352 ashik.ali 430
			}
22471 ashik.ali 431
 
22352 ashik.ali 432
		}
22471 ashik.ali 433
 
434
 
22352 ashik.ali 435
	}
436
 
437
	public void migratePrivateDealToRetailer(){
438
 
439
	}
22125 ashik.ali 440
}