Subversion Repositories SmartDukaan

Rev

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

Rev 6885 Rev 8578
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
import java.io.File;
3
import java.io.File;
-
 
4
import java.io.IOException;
-
 
5
import java.util.ArrayList;
-
 
6
import java.util.Calendar;
-
 
7
import java.util.GregorianCalendar;
4
import java.util.List;
8
import java.util.List;
5
 
9
 
6
import org.apache.log4j.Logger;
10
import org.apache.log4j.Logger;
7
import org.apache.struts2.convention.annotation.Result;
11
import org.apache.struts2.convention.annotation.Result;
8
import org.apache.struts2.convention.annotation.Results;
12
import org.apache.struts2.convention.annotation.Results;
9
import javax.servlet.http.HttpServletRequest;
13
import javax.servlet.http.HttpServletRequest;
10
import org.apache.commons.io.FileUtils;
14
import org.apache.commons.io.FileUtils;
11
import org.apache.commons.lang.xwork.StringUtils;
15
import org.apache.commons.lang.xwork.StringUtils;
-
 
16
 
-
 
17
import in.shop2020.config.ConfigException;
12
import in.shop2020.model.v1.catalog.Banner;
18
import in.shop2020.model.v1.catalog.Banner;
13
import in.shop2020.model.v1.catalog.BannerMap;
19
import in.shop2020.model.v1.catalog.BannerMap;
-
 
20
import in.shop2020.model.v1.catalog.BannerUriMapping;
-
 
21
import in.shop2020.model.v1.catalog.Campaign;
14
import in.shop2020.model.v1.catalog.CatalogService.Client;
22
import in.shop2020.model.v1.catalog.CatalogService.Client;
15
import in.shop2020.serving.services.ScpService;
23
import in.shop2020.serving.services.ScpService;
16
import in.shop2020.thrift.clients.CatalogClient;
24
import in.shop2020.thrift.clients.CatalogClient;
17
import org.apache.thrift.TException;
25
import org.apache.thrift.TException;
18
import org.apache.thrift.transport.TTransportException;
26
import org.apache.thrift.transport.TTransportException;
19
 
27
 
20
 
28
 
21
@SuppressWarnings("serial")
29
@SuppressWarnings("serial")
22
@Results({
30
@Results({
23
    @Result(name = "redirect", location = "${url}", type = "redirect") })
31
	@Result(name = "redirect", location = "${url}", type = "redirect"),
-
 
32
	@Result(name = "newCampaign", location = "campaign-editNew.vm"),
-
 
33
	@Result(name = "campaignEdit", location = "campaignEdit.vm"),
-
 
34
})
24
   public class UserImageController extends BaseController {
35
public class UserImageController extends BaseController {
25
	private static Logger log = Logger.getLogger(Class.class);
36
	private static Logger log = Logger.getLogger(Class.class);
26
    private File userImage;
37
	private File userImage;
27
    private String filePath;
38
	private String filePath;
28
    private String userImageContentType;
39
	private String userImageContentType;
29
    private String userImageFileName;
40
	private String userImageFileName;
30
    private String url;
41
	private String url;
31
    private HttpServletRequest servletRequest;
42
	private HttpServletRequest servletRequest;
32
    private String status;
43
	private String[] status;
33
    private String bannerName;
44
	private String bannerName;
34
    private String link;
45
	private String link;
35
    private String priority;
46
	private String priority;
36
    private String hasMap;
47
	private String hasMap;
37
    private String action;
48
	private String action;
38
    private String imageName;
49
	private String[] mapLink;
-
 
50
	private String[] mapCoordinates;
39
    private String[] mapLink;
51
	private String[] uri;
-
 
52
	private String id;
-
 
53
	private String newsletterName;
40
    private String[] mapCoordinates;
54
	private String imageName;
-
 
55
	private String campaignId;
41
    public String create() {
56
	public String create() {
42
        try {
57
		try {
43
            filePath = servletRequest.getSession().getServletContext().getRealPath("/images/banners/");
58
			filePath = servletRequest.getSession().getServletContext().getRealPath("/images/banners/");
44
            setUrl("/user-image");
59
			setUrl("/user-image");
-
 
60
			String imageName = null;
45
            if (StringUtils.isNotEmpty(userImageFileName)) {
61
			if (StringUtils.isNotEmpty(userImageFileName)) {
-
 
62
				imageName = getImageName();
46
            File fileToCreate = new File(filePath, this.imageName); 
63
				File fileToCreate = new File(filePath, imageName); 
47
            FileUtils.copyFile(this.userImage, fileToCreate);
64
				FileUtils.copyFile(this.userImage, fileToCreate);
48
            try {
65
				try {
49
                ScpService scp = new ScpService();
66
					ScpService scp = new ScpService();
50
                scp.scpFile("/var/lib/tomcat6/webapps/ROOT/images/banners/"+imageName, filePath+"/"+imageName);
67
					scp.scpFile("/var/lib/tomcat6/webapps/ROOT/images/banners/"+imageName, filePath+"/"+imageName);
51
                } catch (Exception e) {
68
				} catch (Exception e) {
52
                	log.error("Scp To Servers Failed",e);
69
					log.error("Scp To Servers Failed",e);
53
                	addActionError("Image Failed To Upload");
70
					addActionError("Image Failed To Upload");
54
                	return "redirect";
71
					return "redirect";
55
                }
72
				}
-
 
73
			}
56
            }
74
			else{
-
 
75
				Client CatalogClient = new CatalogClient().getClient();
-
 
76
				imageName =CatalogClient.getBannerDetails(action).getImageName(); 
-
 
77
			}
57
            if (StringUtils.isEmpty(link)) {
78
			if (StringUtils.isEmpty(link)) {
58
            	log.info("Empty Link Field");
79
				log.info("Empty Link Field");
59
            	this.link="#";
80
				this.link="#";
60
            }
81
			}
61
            long rating = Long.parseLong(priority);
82
			Banner banner = new Banner();
62
            boolean state = Boolean.parseBoolean(status);
83
			banner.setBannerName(bannerName);
63
            boolean containsMap = Boolean.parseBoolean(hasMap);
84
			banner.setImageName(imageName);
-
 
85
			banner.setLink(link);
64
            log.info("Adding banner: "+"Banner Name"+bannerName+"Image Name"+imageName+"Link"+link+"Rating"+rating+"Status"+state+"Has Map"+containsMap);
86
			banner.setPriority(Long.parseLong(priority));
65
            Client CatalogClient = new CatalogClient().getClient();
87
			banner.setHasMap(Boolean.parseBoolean(hasMap));
66
            if (StringUtils.isNotEmpty(action)) {
88
			if (StringUtils.isNotEmpty(action)) {
67
            	CatalogClient.deleteBanner(action);
-
 
68
            	CatalogClient.deleteBannerMap(action);
89
				deleteBannerAndMap(action);
69
            }
90
			}
70
            CatalogClient.addBanner(bannerName, imageName,link, rating, state, containsMap);
91
			List<BannerMap> bannerMaps = new ArrayList<BannerMap>();
71
        	if(containsMap) {
92
			if(banner.isHasMap()) {
72
        		log.info("Found Map");
-
 
73
        	   for(int i=0;i<mapLink.length;i++){
93
				for(int i=0;i<mapLink.length;i++){
74
        		   log.info("maplink"+mapLink[i]);
94
					log.info("maplink"+mapLink[i]);
75
        		   log.info("maplink"+mapCoordinates[i]);
95
					log.info("maplink"+mapCoordinates[i]);
76
    			   if(mapLink[i].isEmpty()) {
96
					if(mapLink[i].isEmpty()) {
77
    				  continue;
97
						continue;
78
    			   }
98
					}
-
 
99
					BannerMap bannerMap = new BannerMap();
-
 
100
					bannerMap.setBannerName(bannerName);
79
    	 	       CatalogClient.addBannerMap(bannerName, mapLink[i], mapCoordinates[i]);
101
					bannerMap.setCoordinates(mapCoordinates[i]);
-
 
102
					bannerMap.setMapLink(mapLink[i]);
-
 
103
					bannerMaps.add(bannerMap);
-
 
104
				}	
-
 
105
			}
-
 
106
			Client CatalogClient = new CatalogClient().getClient();
-
 
107
			CatalogClient.addBanner(banner);
-
 
108
			if(bannerMaps.size()>0){
-
 
109
				addBannerMap(bannerMaps);
-
 
110
			}
-
 
111
			List<BannerUriMapping> bannerUriMappings = new ArrayList<BannerUriMapping>();
-
 
112
			for (int i=0;i<uri.length;i++){
-
 
113
				if(uri[i].isEmpty()) {
80
         	   }	
114
					continue;
81
        	}
115
				}
-
 
116
				BannerUriMapping bannerUriMapping = new BannerUriMapping();
-
 
117
				bannerUriMapping.setBannerName(bannerName);
-
 
118
				bannerUriMapping.setUri(uri[i]);
-
 
119
				bannerUriMapping.setIsActive(Boolean.valueOf(status[i]));
-
 
120
				bannerUriMappings.add(bannerUriMapping);
-
 
121
			}
-
 
122
			addBannerUriMapping(bannerUriMappings);
82
        } catch (Exception e) {   
123
		} catch (Exception e) {   
83
        	log.error("Error while uploading/changing map",e);
124
			log.error("Error while uploading/changing map",e);
84
            addActionError("Failed To Do Changes");
125
			addActionError("Failed To Do Changes");
85
            return "redirect";
126
			return "redirect";
86
        }
127
		}
87
        addActionMessage("Changes Applied Successfully");
128
		addActionMessage("Changes Applied Successfully");
88
        return "redirect";
129
		return "index";
-
 
130
	}
-
 
131
 
-
 
132
	public String createNewsletter() throws IOException{
-
 
133
		filePath = servletRequest.getSession().getServletContext().getRealPath("/images/campaignImages/");
-
 
134
		if (StringUtils.isNotEmpty(userImageFileName)) {
-
 
135
			File fileToCreate = new File(filePath, imageName); 
-
 
136
			FileUtils.copyFile(this.userImage, fileToCreate);
-
 
137
			ScpService scp = new ScpService();
-
 
138
			try {
-
 
139
				scp.scpFile("/var/lib/tomcat6/webapps/ROOT/images/campaignImages/"+imageName, filePath+"/"+imageName);
-
 
140
			} catch (ConfigException e) {
-
 
141
				log.info("Unable to do scp "+e);
-
 
142
				addActionError("Failed To Do Changes");
-
 
143
				return "campaign-editNew";
89
    }
144
			}
-
 
145
			Campaign campaign = new Campaign();
-
 
146
			campaign.setCampaignName(this.newsletterName);
-
 
147
			campaign.setImageName(imageName);
-
 
148
			Client CatalogClient;
-
 
149
			try {
-
 
150
				CatalogClient = new CatalogClient().getClient();
-
 
151
				CatalogClient.addCampaign(campaign);
-
 
152
			} catch (Exception e) {
-
 
153
				log.info("Unable to add campaign "+e);
-
 
154
				addActionError("Failed To Do Changes");
-
 
155
				return "campaign-editNew";
90
    
156
			}
-
 
157
		}
-
 
158
		addActionMessage("Changes Applied Successfully");
-
 
159
		return "campaign-editNew";
-
 
160
	}
-
 
161
 
-
 
162
	public void addBannerUriMapping(List<BannerUriMapping> bannerUriMappings) throws TException{
-
 
163
		Client CatalogClient = new CatalogClient().getClient();
-
 
164
		CatalogClient.addBannerUri(bannerUriMappings);
-
 
165
	}
-
 
166
 
-
 
167
	public void addBannerMap(List<BannerMap> bannerMaps) throws TException{
-
 
168
		Client CatalogClient = new CatalogClient().getClient();
-
 
169
		CatalogClient.addBannerMap(bannerMaps);
-
 
170
	}
-
 
171
 
91
    public List<String> getAllBanners() throws TException{
172
	public List<Banner> getAllBanners() {
92
    	Client CatalogClient = null;
173
		Client CatalogClient = null;
-
 
174
		List<Banner> banners = null;
93
		try {
175
		try {
94
			CatalogClient = new CatalogClient().getClient();
176
			CatalogClient = new CatalogClient().getClient();
-
 
177
			banners = CatalogClient.getAllBanners();
95
		} catch (TTransportException e) {
178
		} catch (TTransportException e) {
-
 
179
			log.info("Unable to fetch all banners "+e);
96
			e.printStackTrace();
180
		} catch (TException ex) {
-
 
181
			// TODO Auto-generated catch block
-
 
182
			log.info("Unable to fetch all banners "+ex);
97
		}
183
		}
98
    	return CatalogClient.getAllBanners();	
184
		return banners;	
99
    }
185
	}
100
    
186
 
101
    public String destroy() throws TException {
187
	public String destroy() throws TException {
102
    	Client CatalogClient = new CatalogClient().getClient();
188
		Client CatalogClient = new CatalogClient().getClient();
103
    	CatalogClient.deleteBanner(bannerName);
189
		CatalogClient.deleteBanner(bannerName);
104
    	CatalogClient.deleteBannerMap(bannerName);
-
 
105
    	setUrl("/user-image");
190
		setUrl("/user-image");
106
    	return "redirect";
191
		return "redirect";
107
    	
192
 
108
    }
193
	}
109
    
194
 
110
    public Banner getUpdates() throws TException {
195
	public Banner getUpdates() throws TException {
111
    	Client CatalogClient = null;
196
		Client CatalogClient = null;
112
		CatalogClient = new CatalogClient().getClient();
197
		CatalogClient = new CatalogClient().getClient();
113
		return CatalogClient.getBannerDetails(bannerName);
198
		return CatalogClient.getBannerDetails(bannerName);
114
    } 
199
	} 
115
    
200
 
116
    public Banner getUpdates(String name) throws TException {
201
	public Banner getUpdates(String name) throws TException {
117
    	Client CatalogClient = null;
202
		Client CatalogClient = null;
118
		CatalogClient = new CatalogClient().getClient();
203
		CatalogClient = new CatalogClient().getClient();
119
		return CatalogClient.getBannerDetails(name);
204
		return CatalogClient.getBannerDetails(name);
120
    } 
205
	} 
121
    
206
 
122
    public List<BannerMap> getbannermapdetails() throws TException {
207
	public List<BannerMap> getbannermapdetails() throws TException {
123
    	Client CatalogClient = null;
208
		Client CatalogClient = null;
124
    	CatalogClient = new CatalogClient().getClient();
209
		CatalogClient = new CatalogClient().getClient();
125
    	return CatalogClient.getBannerMapDetails(bannerName);
210
		return CatalogClient.getBannerMapDetails(bannerName);
-
 
211
	}
-
 
212
 
-
 
213
	public String getImageName() {
-
 
214
		Calendar cal=GregorianCalendar.getInstance();
-
 
215
		return String.valueOf(cal.getTimeInMillis())+".jpg";
-
 
216
	}
-
 
217
 
-
 
218
	public List<BannerUriMapping> getUriMapping(String bannerName) throws TException{
-
 
219
		Client CatalogClient = new CatalogClient().getClient();
-
 
220
		return CatalogClient.getUriMapping(bannerName);
-
 
221
	}
-
 
222
 
-
 
223
	public void deleteBannerAndMap(String bannerName) throws TException{
-
 
224
		Client CatalogClient = new CatalogClient().getClient();
-
 
225
		CatalogClient.deleteBanner(bannerName);
-
 
226
	}
-
 
227
 
-
 
228
	public List<BannerUriMapping> getUriMapping() throws TException{
-
 
229
		Client CatalogClient = new CatalogClient().getClient();
-
 
230
		return CatalogClient.getUriMapping(bannerName);
-
 
231
	}
-
 
232
 
-
 
233
	public List<Campaign> getCampaigns(String campaignName) throws TException{
-
 
234
		Client CatalogClient = new CatalogClient().getClient();
-
 
235
		return CatalogClient.getCampaigns(campaignName);
-
 
236
	}
-
 
237
 
-
 
238
	public void deleteCampaign() throws NumberFormatException, TException{
-
 
239
		Client CatalogClient = new CatalogClient().getClient();
-
 
240
		CatalogClient.deleteCampaign(Long.valueOf(campaignId));
126
    }
241
	}
-
 
242
 
-
 
243
	public List<String> getAllCampaigns() throws TException{
-
 
244
		Client CatalogClient = new CatalogClient().getClient();
-
 
245
		log.info(CatalogClient.getAllCampaigns());
-
 
246
		return CatalogClient.getAllCampaigns();
127
    
247
	}
-
 
248
 
128
    public String index() {
249
	public String index() {
129
        return "index";
250
		return "index";
130
    }
251
	}
131
 
252
 
132
    public String show() {
253
	public String show() {
-
 
254
		if (StringUtils.equals(id, "newCampaign")){
-
 
255
			log.info("Returing camapign edit new");
-
 
256
			return "newCampaign";
-
 
257
		}
133
        return "show";
258
		return "show";
134
    }
259
	}
-
 
260
 
-
 
261
	public String editNewsletter(){
-
 
262
		return "campaignEdit";
-
 
263
	}
135
 
264
 
136
    public String edit() {
265
	public String edit() {
137
        return "edit";
266
		return "edit";
138
    }
267
	}
139
    
268
 
140
    
269
 
141
    public String editNew() {
270
	public String editNew() {
142
        return "editNew";
271
		return "editNew";
143
    }
272
	}
144
 
273
 
145
    public File getUserImage() {
274
	public File getUserImage() {
146
        return userImage;
275
		return userImage;
147
    }
276
	}
148
 
277
 
149
    public void setUserImage(File userImage) {
278
	public void setUserImage(File userImage) {
-
 
279
		log.info("set file"+userImage.toString());
150
        this.userImage = userImage;
280
		this.userImage = userImage;
151
    }
281
	}
152
 
282
 
153
    public String getUserImageContentType() {
283
	public String getUserImageContentType() {
154
        return userImageContentType;
284
		return userImageContentType;
155
    }
285
	}
156
 
286
 
157
    public void setUserImageContentType(String userImageContentType) {
287
	public void setUserImageContentType(String userImageContentType) {
158
    	this.userImageContentType = userImageContentType;
288
		this.userImageContentType = userImageContentType;
159
    }
289
	}
160
 
290
 
161
    public String getUserImageFileName() {
291
	public String getUserImageFileName() {
162
        return userImageFileName;
292
		return userImageFileName;
163
    }
293
	}
164
 
294
 
165
    public void setUserImageFileName(String userImageFileName) {
295
	public void setUserImageFileName(String userImageFileName) {
-
 
296
		log.info("userimage file name set"+userImageFileName);
166
        this.userImageFileName = userImageFileName;
297
		this.userImageFileName = userImageFileName;
167
    }
298
	}
168
    
299
 
169
    public void setUrl(String url) {
300
	public void setUrl(String url) {
170
		this.url = url;
301
		this.url = url;
171
	}
302
	}
172
 
303
 
173
	public String getUrl() {
304
	public String getUrl() {
174
		return url;
305
		return url;
175
	}
306
	}
176
	
307
 
177
	public void setIsActive(String isActive) {
308
	public void setIsActive(String[] isActive) {
178
		this.status=isActive;
309
		this.status=isActive;
179
	}
310
	}
180
	
311
 
181
	public String getIsActive() {
312
	public String[] getIsActive() {
182
		return status;
313
		return status;
183
	}
314
	}
184
	
315
 
185
	
316
 
186
	public void setBannerName(String bannerName) {
317
	public void setBannerName(String bannerName) {
187
		this.bannerName=bannerName;
318
		this.bannerName=bannerName;
188
	}
319
	}
189
	
320
 
190
	public String getBannerName() {
321
	public String getBannerName() {
191
		return bannerName;
322
		return bannerName;
192
	}
323
	}
193
	
324
 
194
	public void setLink(String link) {
325
	public void setLink(String link) {
195
		this.link=link;
326
		this.link=link;
196
	}
327
	}
197
	
328
 
198
	public String getLink() {
329
	public String getLink() {
199
		return link;
330
		return link;
200
	}
331
	}
201
	
332
 
202
	public void setAction(String action) {
333
	public void setAction(String action) {
203
		this.action=action;
334
		this.action=action;
204
	}
335
	}
205
	
336
 
206
	public String getAction() {
337
	public String getAction() {
207
		return action;
338
		return action;
208
	}
339
	}
209
	
340
 
210
	public void setPriority(String priority) {
341
	public void setPriority(String priority) {
211
		this.priority=priority;
342
		this.priority=priority;
212
	}
343
	}
213
	
344
 
214
	public String getPriority() {
345
	public String getPriority() {
215
		return priority;
346
		return priority;
216
	}
347
	}
217
	
-
 
218
	public void setImageName(String imageName) {
-
 
219
		this.imageName=imageName;
-
 
220
	}
-
 
221
	
348
 
222
	public String getImageName() {
-
 
223
		return imageName;
-
 
224
	}
-
 
225
	
-
 
226
	public void setId(String id) {
349
	public void setId(String id) {
-
 
350
		log.info("set id banner "+id);
-
 
351
		this.id = id;
227
        this.bannerName = id;
352
		this.bannerName = id;
228
    }
353
	}
229
	
354
 
230
	public String[] getMapLink() {
355
	public String[] getMapLink() {
231
		return mapLink;
356
		return mapLink;
232
	}
357
	}
233
	public void setMapLink(String[] mapLink) {
358
	public void setMapLink(String[] mapLink) {
234
		this.mapLink = mapLink;
359
		this.mapLink = mapLink;
235
	}
360
	}
236
	public String[] getReturnQty() {
361
	public String[] getMapCoordinates() {
237
		return mapCoordinates;
362
		return mapCoordinates;
238
	}
363
	}
239
	public void setMapCoordinates(String[] mapCoordinates) {
364
	public void setMapCoordinates(String[] mapCoordinates) {
240
		this.mapCoordinates = mapCoordinates;
365
		this.mapCoordinates = mapCoordinates;
241
	}
366
	}
-
 
367
 
-
 
368
	public void setUri(String[] uri) {
-
 
369
		this.uri = uri;
242
	
370
	}
-
 
371
 
-
 
372
	public String[] getUri() {
-
 
373
		return uri;
-
 
374
	}
-
 
375
 
243
	public void setHasMap(String hasMap) {
376
	public void setHasMap(String hasMap) {
244
		this.hasMap=hasMap;
377
		this.hasMap=hasMap;
245
	}
378
	}
246
	
379
 
247
	public String getHasMap() {
380
	public String getHasMap() {
248
		return hasMap;
381
		return hasMap;
249
	}
382
	}
-
 
383
 
-
 
384
	public void setNewsletterName(String newsletterName) {
-
 
385
		this.newsletterName = newsletterName;
250
	
386
	}
-
 
387
 
-
 
388
	public String getNewsletterName() {
-
 
389
		return newsletterName;
-
 
390
	}
-
 
391
 
-
 
392
	public void setCampaignId(String campaignId) {
-
 
393
		this.campaignId = campaignId;
-
 
394
	}
-
 
395
 
-
 
396
	public String getCampaignId() {
-
 
397
		return campaignId;
251
    
398
	}
-
 
399
 
252
    @Override
400
	@Override
253
    public void setServletRequest(HttpServletRequest servletRequest) {
401
	public void setServletRequest(HttpServletRequest servletRequest) {
254
        this.servletRequest = servletRequest;
402
		this.servletRequest = servletRequest;
-
 
403
 
-
 
404
	}
255
 
405
 
-
 
406
	public void setImageName(String imageName) {
-
 
407
		this.imageName = imageName;
256
    }
408
	}
-
 
409
 
257
}
410
}
258
411