Subversion Repositories SmartDukaan

Rev

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

Rev 21315 Rev 21390
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
3
import java.time.LocalDateTime;
3
import java.time.LocalDateTime;
4
import java.util.Map;
4
import java.util.Map;
-
 
5
import java.util.Set;
5
 
6
 
6
import javax.servlet.http.HttpServletRequest;
7
import javax.servlet.http.HttpServletRequest;
7
 
8
 
8
import org.slf4j.Logger;
9
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10
import org.slf4j.LoggerFactory;
Line 17... Line 18...
17
import org.springframework.web.bind.annotation.RequestParam;
18
import org.springframework.web.bind.annotation.RequestParam;
18
 
19
 
19
import com.spice.profitmandi.common.ResponseCodeHolder;
20
import com.spice.profitmandi.common.ResponseCodeHolder;
20
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22
import com.spice.profitmandi.common.model.ProfitMandiConstants;
-
 
23
import com.spice.profitmandi.dao.entity.Address;
-
 
24
import com.spice.profitmandi.dao.entity.Brand;
-
 
25
import com.spice.profitmandi.dao.entity.Document;
22
import com.spice.profitmandi.dao.entity.Retailer;
26
import com.spice.profitmandi.dao.entity.Retailer;
-
 
27
import com.spice.profitmandi.dao.entity.RetailerBrand;
-
 
28
import com.spice.profitmandi.dao.entity.RetailerRegisteredAddress;
-
 
29
import com.spice.profitmandi.dao.entity.Shop;
-
 
30
import com.spice.profitmandi.dao.entity.ShopAddress;
-
 
31
import com.spice.profitmandi.dao.enumuration.RetailerType;
-
 
32
import com.spice.profitmandi.dao.enumuration.SaleValue;
-
 
33
import com.spice.profitmandi.dao.repository.AddressRepository;
-
 
34
import com.spice.profitmandi.dao.repository.BrandRepository;
-
 
35
import com.spice.profitmandi.dao.repository.DocumentRepository;
-
 
36
import com.spice.profitmandi.dao.repository.RetailerBrandRepository;
-
 
37
import com.spice.profitmandi.dao.repository.RetailerRegisteredAddressRepository;
23
import com.spice.profitmandi.dao.repository.RetailerRepository;
38
import com.spice.profitmandi.dao.repository.RetailerRepository;
-
 
39
import com.spice.profitmandi.dao.repository.ShopAddressRepository;
24
import com.spice.profitmandi.web.model.CreateRetailerRequestModel;
40
import com.spice.profitmandi.dao.repository.ShopRepository;
25
import com.spice.profitmandi.web.model.ProfitMandiResponse;
41
import com.spice.profitmandi.web.model.ProfitMandiResponse;
26
import com.spice.profitmandi.web.model.Response;
42
import com.spice.profitmandi.web.model.Response;
27
import com.spice.profitmandi.web.model.ResponseStatus;
43
import com.spice.profitmandi.web.model.ResponseStatus;
-
 
44
import com.spice.profitmandi.web.req.CreateRetailerRequestModel;
28
 
45
 
29
import io.swagger.annotations.ApiImplicitParam;
46
import io.swagger.annotations.ApiImplicitParam;
30
import io.swagger.annotations.ApiImplicitParams;
47
import io.swagger.annotations.ApiImplicitParams;
31
import io.swagger.annotations.ApiOperation;
48
import io.swagger.annotations.ApiOperation;
32
 
49
 
Line 36... Line 53...
36
	private static final Logger LOGGER=LoggerFactory.getLogger(RetailerController.class);
53
	private static final Logger LOGGER=LoggerFactory.getLogger(RetailerController.class);
37
	
54
	
38
	@Autowired
55
	@Autowired
39
	RetailerRepository retailerRepository;
56
	RetailerRepository retailerRepository;
40
	
57
	
-
 
58
	@Autowired
-
 
59
	BrandRepository brandRepository;
-
 
60
	
-
 
61
	@Autowired
-
 
62
	AddressRepository addressRepository;
-
 
63
	
-
 
64
	@Autowired
-
 
65
	DocumentRepository documentRepository;
-
 
66
	
-
 
67
	@Autowired
-
 
68
	ShopRepository shopRepository;
-
 
69
	
-
 
70
	@Autowired
-
 
71
	RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
-
 
72
	
-
 
73
	@Autowired
-
 
74
	ShopAddressRepository shopAddressRepository;
-
 
75
	
-
 
76
	@Autowired
-
 
77
	RetailerBrandRepository retailerBrandRepository;
-
 
78
	
-
 
79
	@SuppressWarnings("unchecked")
41
	@ApiImplicitParams({
80
	@ApiImplicitParams({
42
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
81
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
43
				required = true, dataType = "string", paramType = "header")
82
				required = true, dataType = "string", paramType = "header")
44
	})
83
	})
45
	
84
	
46
	@ApiOperation(value = "Create Retailer")
85
	@ApiOperation(value = "Create Retailer")
47
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER, method=RequestMethod.POST)
86
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER, method=RequestMethod.POST)
48
	public ResponseEntity<?> createRetailer(HttpServletRequest request, @RequestBody CreateRetailerRequestModel createRetailerRequestModel){
87
	public ResponseEntity<?> createRetailer(HttpServletRequest request, @RequestBody CreateRetailerRequestModel createRetailerRequestModel){
49
		LOGGER.info("requested url : "+request.getRequestURL().toString());
88
		LOGGER.info("requested url : "+request.getRequestURL().toString());
50
		final Retailer retailer = (Retailer)request.getAttribute(ProfitMandiConstants.RETAILER);
89
		final Map<String, Object> createRetailerMap = (Map<String, Object>)request.getAttribute(ProfitMandiConstants.CREATE_RETAILER_MAP);
51
		request.removeAttribute(ProfitMandiConstants.RETAILER);
90
		request.removeAttribute(ProfitMandiConstants.CREATE_RETAILER_MAP);
-
 
91
		Retailer retailer = new Retailer();
-
 
92
		retailer.setName(createRetailerMap.get(ProfitMandiConstants.NAME).toString());
-
 
93
		retailer.setNumber(createRetailerMap.get(ProfitMandiConstants.NUMBER).toString());
-
 
94
		retailer.setType((RetailerType)createRetailerMap.get(ProfitMandiConstants.TYPE));
-
 
95
		retailer.setMonthlySaleValue((SaleValue)createRetailerMap.get(ProfitMandiConstants.MONTLY_SALE_VALUE));
-
 
96
		retailer.setSmartphoneSaleValue((SaleValue)createRetailerMap.get(ProfitMandiConstants.SMARTPHONE_SALE_VALUE));
-
 
97
		retailer.setRecharge((boolean)createRetailerMap.get(ProfitMandiConstants.RECHARGE));
-
 
98
		retailer.setMobile((boolean)createRetailerMap.get(ProfitMandiConstants.MOBILE));
-
 
99
		retailer.setAccessories((boolean)createRetailerMap.get(ProfitMandiConstants.ACCESSORITES));
-
 
100
		retailer.setOther1((boolean)createRetailerMap.get(ProfitMandiConstants.OTHER1));
-
 
101
		retailer.setOther2((boolean)createRetailerMap.get(ProfitMandiConstants.OTHER2));
-
 
102
		Set<String> brandNames = (Set<String>) createRetailerMap.get(ProfitMandiConstants.BRAND_NAMES);
-
 
103
		
52
		try {
104
		try {
-
 
105
			
-
 
106
			Document retailerDocument = documentRepository.selectById((int)createRetailerMap.get(ProfitMandiConstants.DOCUMENT_ID));
53
			retailer.setCreateTimestamp(LocalDateTime.now());
107
			if(retailerRepository.isExistByDocumentId(retailerDocument.getId())){
-
 
108
				LOGGER.error("documet is already mapped with another retailer");
-
 
109
				throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, retailerDocument.getId(), "");
-
 
110
			}
54
			retailer.setUpdateTimestamp(LocalDateTime.now());
111
			retailer.setDocumentId(retailerDocument.getId());
55
			retailerRepository.persist(retailer);
112
			retailerRepository.persist(retailer);
-
 
113
			Map<String, Object> createShopMap = (Map<String, Object>)createRetailerMap.get(ProfitMandiConstants.SHOP);
-
 
114
			final Document shopDocument = documentRepository.selectById((int)createShopMap.get(ProfitMandiConstants.DOCUMENT_ID));
-
 
115
			if(shopRepository.isExistByDocumentId(shopDocument.getId())){
-
 
116
				throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, shopDocument.getId(), "");
-
 
117
			}
-
 
118
			Shop shop = new Shop();
-
 
119
			shop.setName(createShopMap.get(ProfitMandiConstants.NAME).toString());
-
 
120
			shop.setDocumentId(shopDocument.getId());
-
 
121
			shop.setRetailerId(retailer.getId());
-
 
122
			try{
-
 
123
				shopRepository.persist(shop);
-
 
124
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
-
 
125
				
-
 
126
			}
-
 
127
			for(String brandName : brandNames){
-
 
128
				Brand brand =new Brand();
-
 
129
				brand.setName(brandName);
-
 
130
				if(!brandRepository.isExistByName(brandName)){
-
 
131
					brandRepository.persist(brand);
-
 
132
				}
-
 
133
				final RetailerBrand retailerBrand = new RetailerBrand();
-
 
134
				retailerBrand.setRetailerId(retailer.getId());
-
 
135
				retailerBrand.setBrandId(brand.getId());
-
 
136
				retailerBrandRepository.persist(retailerBrand);
-
 
137
			}
-
 
138
			final Address addressRetailer = (Address)createRetailerMap.get(ProfitMandiConstants.ADDRESS);
-
 
139
			if(!addressRepository.isExist(addressRetailer)){
-
 
140
				addressRepository.persist(addressRetailer);
-
 
141
			}
-
 
142
			final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
-
 
143
			retailerRegisteredAddress.setRetailerId(retailer.getId());
-
 
144
			retailerRegisteredAddress.setAddressId(addressRetailer.getId());
-
 
145
			try{
-
 
146
				retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
-
 
147
			}catch (ProfitMandiBusinessException profitMandiBusinessException) {
-
 
148
				
-
 
149
			}
-
 
150
			
-
 
151
			final Address addressShop = (Address)createShopMap.get(ProfitMandiConstants.ADDRESS);
-
 
152
			if(!addressRepository.isExist(addressShop)){
-
 
153
				addressRepository.persist(addressShop);
-
 
154
				ShopAddress shopAddress = new ShopAddress();
-
 
155
				shopAddress.setShopId(shop.getId());
-
 
156
				shopAddress.setAddressId(addressShop.getId());
-
 
157
				try{
-
 
158
					shopAddressRepository.persist(shopAddress);
-
 
159
				}catch(ProfitMandiBusinessException profitMandiBusinessException){
-
 
160
					
-
 
161
				}
-
 
162
			}
-
 
163
			ShopAddress shopAddress = new ShopAddress();
-
 
164
			shopAddress.setShopId(shop.getId());
-
 
165
			shopAddress.setAddressId(addressShop.getId());
-
 
166
			try{
-
 
167
				shopAddressRepository.persist(shopAddress);
-
 
168
			}catch(ProfitMandiBusinessException profitMandiBusinessException){
-
 
169
				
-
 
170
			}
56
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("RTLR_OK_1000"));
171
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("RTLR_OK_1000"));
57
			return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
172
			return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
58
		}catch (ProfitMandiBusinessException pmbe) {
173
		}catch (ProfitMandiBusinessException pmbe) {
59
			LOGGER.error("ProfitMandi error: ", pmbe);
174
			LOGGER.error("ProfitMandi error: ", pmbe);
60
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
175
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
Line 80... Line 195...
80
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
195
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
81
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
196
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
82
		}
197
		}
83
	}
198
	}
84
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID, method=RequestMethod.GET)
199
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID, method=RequestMethod.GET)
85
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") long id){
200
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
86
		LOGGER.info("requested url : "+request.getRequestURL().toString());
201
		LOGGER.info("requested url : "+request.getRequestURL().toString());
87
		try {
202
		try {
88
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectById(id));
203
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectById(id));
89
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
204
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
90
		}catch (ProfitMandiBusinessException pmbe) {
205
		}catch (ProfitMandiBusinessException pmbe) {
Line 119... Line 234...
119
		}
234
		}
120
	}
235
	}
121
		
236
		
122
	
237
	
123
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)
238
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)
124
	public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") long id){
239
	public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") int id){
125
		LOGGER.info("requested url : "+request.getRequestURL().toString());
240
		LOGGER.info("requested url : "+request.getRequestURL().toString());
126
		try {
241
		try {
127
			retailerRepository.deleteById(id);
242
			retailerRepository.deleteById(id);
128
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
243
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
129
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
244
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
Line 139... Line 254...
139
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
254
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
140
		}
255
		}
141
	}
256
	}
142
	
257
	
143
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_REMOVE, method=RequestMethod.DELETE)
258
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_REMOVE, method=RequestMethod.DELETE)
144
	public ResponseEntity<?> removeShop(HttpServletRequest request, @RequestParam(name = "shopId") long shopId, @RequestParam(name = "retailerId") long retailerId){
259
	public ResponseEntity<?> removeShop(HttpServletRequest request, @RequestParam(name = "shopId") int shopId, @RequestParam(name = "retailerId") int retailerId){
145
		LOGGER.info("requested url : "+request.getRequestURL().toString());
260
		LOGGER.info("requested url : "+request.getRequestURL().toString());
146
		try {
261
		try {
147
			retailerRepository.removeShop(shopId, retailerId);
262
			retailerRepository.removeShop(shopId, retailerId);
148
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
263
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
149
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
264
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);