Subversion Repositories SmartDukaan

Rev

Rev 22770 | Rev 22886 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 22770 Rev 22868
Line 54... Line 54...
54
 
54
 
55
@Component
55
@Component
56
public class MigrationUtil {
56
public class MigrationUtil {
57
	
57
	
58
	@Autowired
58
	@Autowired
59
	DocumentRepository documentRepository;
59
	private DocumentRepository documentRepository;
60
	
60
	
61
	@Autowired
61
	@Autowired
62
	UserRepository userRepository;
62
	private UserRepository userRepository;
63
	
63
	
64
	@Autowired
64
	@Autowired
65
	com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
65
	private com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
66
	
66
	
67
	@Autowired
67
	@Autowired
68
	CartRepository cartRepository;
68
	private CartRepository cartRepository;
69
	
69
	
70
	@Autowired
70
	@Autowired
71
	UserAccountRepository userAccountRepository;
71
	private UserAccountRepository userAccountRepository;
72
	
72
	
73
	@Autowired
73
	@Autowired
74
	RetailerRepository retailerRepository;
74
	private RetailerRepository retailerRepository;
75
	
75
	
76
	@Autowired
76
	@Autowired
77
	UserRoleRepository userRoleRepository;
77
	private UserRoleRepository userRoleRepository;
78
	
78
	
79
	@Autowired
79
	@Autowired
80
	AddressRepository addressRepository;
80
	private AddressRepository addressRepository;
81
	
81
	
82
	@Autowired
82
	@Autowired
83
	RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
83
	private RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
84
	
84
	
85
	@Autowired
85
	@Autowired
86
	PrivateDealUserRepository privateDealUserRepository;
86
	private PrivateDealUserRepository privateDealUserRepository;
87
	
87
	
88
	@Autowired
88
	@Autowired
89
	PrivateDealUserAddressMappingRepository privateDealUserAddressMappingRepository;
89
	private PrivateDealUserAddressMappingRepository privateDealUserAddressMappingRepository;
90
	
90
	
91
	@Autowired
91
	@Autowired
92
	CounterRepository counterRepository;
92
	private CounterRepository counterRepository;
93
	
93
	
94
	@Autowired
94
	@Autowired
95
	Mongo mongoClient;
95
	private Mongo mongoClient;
96
	
96
	
97
	
97
	
98
	private static final Logger LOGGER = LoggerFactory.getLogger(MigrationUtil.class);
98
	private static final Logger LOGGER = LoggerFactory.getLogger(MigrationUtil.class);
99
	
99
	
100
	private List<Map<String, String>> getMongoFofoDoc(){
100
	private List<Map<String, String>> getMongoFofoDoc(){
Line 150... Line 150...
150
        	mongoClient.updateColumnsById(entry.getValue(), entry.getKey());
150
        	mongoClient.updateColumnsById(entry.getValue(), entry.getKey());
151
        }
151
        }
152
        
152
        
153
	}
153
	}
154
	
154
	
155
	public void migrateUserToRetailer(){
155
	private Set<Integer> toRetailerIds(List<UserAccounts> userAccounts){
156
		Set<Integer> retailersAdded = new HashSet<>();
156
		Set<Integer> retailerIds = new HashSet<>();
157
		List<Integer> userIds = userRepository.selectIdAll();
-
 
158
		List<UserAccounts> saholicUserAccounts = userAccountRepository.selectAllSaholicByUserIds(new HashSet<>(userIds));
-
 
159
		for(UserAccounts saholicUserAccount : saholicUserAccounts){
157
		for(UserAccounts saholicUserAccount : userAccounts){
160
			int retailerId = Integer.parseInt(saholicUserAccount.getAccount_key());
158
			int retailerId = Integer.parseInt(saholicUserAccount.getAccount_key());
161
			if(retailerRepository.isExistById(retailerId)) {
159
			retailerIds.add(retailerId);
162
				LOGGER.info("Already reatiler with retailerId {}", retailerId);
-
 
-
 
160
		}
163
				continue;
161
		return retailerIds;
164
			}
162
	}
-
 
163
	
-
 
164
	private Set<Integer> toUserIds(List<UserAccounts> userAccounts, Set<Integer> retailerIds){
-
 
165
		Set<Integer> userIds = new HashSet<>();
-
 
166
		for(UserAccounts saholicUserAccount : userAccounts){
-
 
167
			int retailerId = Integer.parseInt(saholicUserAccount.getAccount_key());
165
			if(!retailersAdded.add(retailerId)){
168
			if(retailerIds.contains(retailerId)){
166
				LOGGER.info("Duplicate retailerId {}", retailerId);
169
				userIds.add(saholicUserAccount.getUser_id());
167
				continue;
-
 
168
			}
170
			}
-
 
171
		}
-
 
172
		return userIds;
-
 
173
	}
-
 
174
	
-
 
175
	public void migrateUserToRetailer(){
-
 
176
		
-
 
177
		List<UserAccounts> saholicUserAccounts = userAccountRepository.selectAllSaholicByUserIds(new HashSet<>(userRepository.selectIdAll()));
-
 
178
		if(saholicUserAccounts.isEmpty()){
-
 
179
			return;
-
 
180
		}
-
 
181
		Set<Integer> retailerIds = this.toRetailerIds(saholicUserAccounts);
-
 
182
		List<Integer> foundRetailerIds = retailerRepository.selectIdsByIds(retailerIds);
-
 
183
		retailerIds.removeAll(foundRetailerIds);
-
 
184
		for(int retailerId : retailerIds){
169
			Retailer retailer = new Retailer();
185
			Retailer retailer = new Retailer();
170
			try {
186
			try {
171
				retailer.setId(retailerId);
187
				retailer.setId(retailerId);
172
				retailer.setMigrated(true);
188
				retailer.setMigrated(true);
173
				retailerRepository.persist(retailer);
189
				retailerRepository.persist(retailer);
-
 
190
				LOGGER.info("Retailer migrated {}", retailerId);
174
			} catch (ProfitMandiBusinessException e) {
191
			} catch (ProfitMandiBusinessException e) {
175
				LOGGER.info("Error occured while commiting retailer");
192
				LOGGER.info("Error occured while commiting retailer");
176
				e.printStackTrace();
193
				e.printStackTrace();
177
			}
194
			}
178
			
195
		}
-
 
196
		
-
 
197
		Set<Integer> userIds = this.toUserIds(saholicUserAccounts, retailerIds);
-
 
198
		
179
			if(!userRoleRepository.isExistByUserIdAndType(saholicUserAccount.getUser_id(), RoleType.RETAILER)){
199
		List<Integer> foundRetailerRoleUserIds = userRoleRepository.selectUserIdsByUserIdsAndRoleType(userIds, RoleType.RETAILER);
-
 
200
		
-
 
201
		Set<Integer> notFoundRetailerRoleUserIds = new HashSet<>(userIds);
-
 
202
		notFoundRetailerRoleUserIds.removeAll(foundRetailerRoleUserIds);
-
 
203
		
-
 
204
		for(int userId : notFoundRetailerRoleUserIds){
180
				UserRole userRole1 = new UserRole();
205
			UserRole retailerRole = new UserRole();
181
				userRole1.setRoleType(RoleType.RETAILER);
206
			retailerRole.setRoleType(RoleType.RETAILER);
182
				userRole1.setUserId(saholicUserAccount.getUser_id());
207
			retailerRole.setUserId(userId);
183
				try {
208
			try {
184
					userRoleRepository.persist(userRole1);
209
				userRoleRepository.persist(retailerRole);
-
 
210
				LOGGER.info("UserId {} with Retailer Role migrated", userId);
185
				} catch (ProfitMandiBusinessException e) {
211
			} catch (ProfitMandiBusinessException e) {
186
					e.printStackTrace();
212
				e.printStackTrace();
187
				}
-
 
188
			}
213
			}
189
			
214
		}
-
 
215
		
190
			if(!userRoleRepository.isExistByUserIdAndType(saholicUserAccount.getUser_id(), RoleType.USER)){
216
		List<Integer> foundUserRoleUserIds = userRoleRepository.selectUserIdsByUserIdsAndRoleType(userIds, RoleType.USER);
-
 
217
		
-
 
218
		Set<Integer> notFoundUserRoleUserIds = new HashSet<>(userIds);
-
 
219
		notFoundUserRoleUserIds.removeAll(foundUserRoleUserIds);
191
			
220
		
-
 
221
		for(int userId : notFoundUserRoleUserIds){
192
				UserRole userRole2 = new UserRole();
222
			UserRole userRole = new UserRole();
193
				userRole2.setRoleType(RoleType.USER);
223
			userRole.setRoleType(RoleType.USER);
194
				userRole2.setUserId(saholicUserAccount.getUser_id());
224
			userRole.setUserId(userId);
195
				try {
225
			try {
196
					userRoleRepository.persist(userRole2);
226
				userRoleRepository.persist(userRole);
-
 
227
				LOGGER.info("UserId {} with User Role migrated", userId);
197
				} catch (ProfitMandiBusinessException e) {
228
			} catch (ProfitMandiBusinessException e) {
198
					e.printStackTrace();
229
				e.printStackTrace();
199
				}
-
 
200
			}
230
			}
201
			LOGGER.info("Retailer migrated {}", retailerId);
-
 
202
		}
231
		}
-
 
232
		
203
	}
233
	}
204
	
234
	
205
	public void migrateMongoDocToRetailer() throws ProfitMandiBusinessException{
235
	public void migrateMongoDocToRetailer() throws ProfitMandiBusinessException{
206
		List<Map<String, String>> maps = this.getMongoFofoDoc();
236
		List<Map<String, String>> maps = this.getMongoFofoDoc();
207
		//LOGGER.info("mongoFofoDocs {}", maps);
237
		//LOGGER.info("mongoFofoDocs {}", maps);