Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
208 naveen 1
/**
2
 * 
3
 */
4
package in.shop2020.creation.controllers;
5
 
448 rajveer 6
import in.shop2020.creation.util.ContentValidator;
208 naveen 7
import in.shop2020.creation.util.CreationUtils;
8
import in.shop2020.creation.util.Media;
448 rajveer 9
import in.shop2020.metamodel.core.Entity;
10
import in.shop2020.metamodel.core.FreeformContent;
11
import in.shop2020.metamodel.core.Slide;
12
import in.shop2020.metamodel.definitions.Catalog;
13
import in.shop2020.metamodel.definitions.EntityContainer;
14
import in.shop2020.metamodel.util.ExpandedEntity;
208 naveen 15
import in.shop2020.util.DBUtils;
16
import in.shop2020.util.Utils;
17
 
18
import java.io.File;
19
import java.io.FileInputStream;
20
import java.io.FileOutputStream;
21
import java.io.InputStream;
22
import java.io.OutputStream;
242 naveen 23
import java.net.URLDecoder;
455 rajveer 24
import java.util.ArrayList;
208 naveen 25
import java.util.Collection;
26
import java.util.HashMap;
448 rajveer 27
import java.util.List;
208 naveen 28
import java.util.Map;
448 rajveer 29
import java.util.StringTokenizer;
208 naveen 30
 
31
import org.apache.commons.io.IOUtils;
32
import org.apache.juli.logging.Log;
33
import org.apache.juli.logging.LogFactory;
34
import org.apache.struts2.convention.annotation.Result;
35
import org.apache.struts2.convention.annotation.Results;
36
import org.apache.struts2.rest.DefaultHttpHeaders;
37
import org.apache.struts2.rest.HttpHeaders;
455 rajveer 38
import org.apache.struts2.views.xslt.ArrayAdapter;
208 naveen 39
 
40
/**
41
 * @author naveen
42
 *
43
 */
44
@Results({
45
    @Result(name="success", type="redirectAction", 
46
    		params = {"actionName" , "media"}),
47
    @Result(name="redirect", location="${url}", type="redirect")
48
})
49
public class MediaController {
50
 
51
	/**
52
	 * 
53
	 */
54
	private static Log log = LogFactory.getLog(MediaController.class);
55
 
56
	/**
57
	 * 
58
	 */
242 naveen 59
	private String id;
208 naveen 60
	private String entityID;
448 rajveer 61
	private String slideID;
208 naveen 62
	private String label;
63
	private File pic;
64
	private String picContentType;
65
	private String picFileName;
66
	private String fileSystemPath;
67
	private String redirectURL;
68
	private String errorString;
69
	private String youtubeURL;
70
 
71
	private Map<String, Media> rawMedia;
72
 
73
 
74
    // GET /media
75
    public HttpHeaders index() {
76
    	log.info("MediaController.index");
77
 
78
        return new DefaultHttpHeaders("index").disableCaching();
79
    }
80
 
81
    // GET /media
82
    public String create() {
83
    	log.info("MediaController.create");
84
 
85
    	try {
86
    		log.info("getPicFileName:" + this.getPicFileName());
87
    		log.info("getFileSystemPath:" + this.getFileSystemPath());
88
    		log.info("getPicContentType:" + this.getPicContentType());
89
 
90
    		boolean hasdata = false;
91
    		String location = "";
92
    		String type = "";
93
    		if(this.getPic() != null) {
94
    			type = "image";
95
    			location = this.storeFile();
96
    			hasdata = true;
97
    		} 
98
    		else if(this.getYoutubeURL() != null) {
99
    			type = "youtube";
100
    			location = this.getYoutubeURL();
101
    			hasdata = true;
102
    		}
103
 
104
    		log.info("location:" + location);
105
    		log.info("label:" + this.getLabel());
106
 
107
    		if(hasdata) {
108
    			Media media = new Media(this.getLabel(), type, location);
109
 
110
    			if(this.getPic() != null) {
111
	    			media.setFileName(this.getPicFileName());
112
	    			media.setContentType(this.getPicContentType());
113
    			}
114
    			else {
115
	    			media.setFileName("");
116
	    			media.setContentType("");
117
    			}
118
    			this.addMedia(media);
448 rajveer 119
    			log.info("entity ID:" + this.entityID);
120
    			log.info("slide ID:" + this.slideID);
450 rajveer 121
 
122
    			String contentLabel = "";
123
    			log.info("type filename and location :" + media.getType() + "~!~" + media.getFileName() + media.getLocation());
124
 
125
    			if(media.getType().compareTo("image")==0){
126
    				contentLabel = media.getLabel() + "~!~" + media.getFileName();
127
    				log.info("Type is image :" + contentLabel);
128
        		}
129
        		if(media.getType().compareTo("youtube")==0){
130
        			contentLabel = media.getLabel() + "~!~" + media.getLocation();
131
        			log.info("Type is youtube :" + contentLabel);
132
        		}
133
 
134
 
455 rajveer 135
 
136
    			Entity entity = CreationUtils.getEntity(Long.parseLong(this.entityID));
137
 
138
	    		Slide slide = entity.getSlide(Long.parseLong(this.slideID));
139
    			if(slide == null){
140
    				slide = new Slide(Long.parseLong(this.getSlideID()));
141
    				entity.addSlide(slide);
142
    			}
143
    			FreeformContent freeformContent = slide.getFreeformContent();
144
    			if(freeformContent == null){
145
    				freeformContent = new FreeformContent();
146
    				slide.setFreeformContent(freeformContent);
147
    			}
450 rajveer 148
    			freeformContent.addMedia(media.getType(), contentLabel);
448 rajveer 149
    			entity.getSlide(Long.parseLong(slideID)).setFreeformContent(freeformContent);
150
    			updateEntity(entity);
151
    			//CreationUtils.storeEntity(entity);
208 naveen 152
    		}
153
 
154
		} catch (Exception e) {
155
			log.error(CreationUtils.getStackTrace(e));
156
			this.setErrorString(CreationUtils.getStackTrace(e));
157
			return "fatal";
158
		}
159
 
448 rajveer 160
		this.redirectURL = "media?entityID=" + this.getEntityID() + "&slideID=" + this.getSlideID();;
208 naveen 161
 
162
		return "redirect";
163
    }
242 naveen 164
 
165
    // DELETE /entity/1000001
208 naveen 166
    /**
167
     * 
242 naveen 168
     */
169
    public String destroy() {
170
    	log.info("MediaController.destroy");
171
    	try {
172
    		long entityID = Long.parseLong(this.getEntityID());
448 rajveer 173
    		long slideID = Long.parseLong(this.getSlideID());
242 naveen 174
    		String label = this.getId();
175
    		log.info("label:" + label);
448 rajveer 176
    		log.info("slide:" + label);
177
    		log.info("entity:" + label);
242 naveen 178
 
179
    		label = URLDecoder.decode(label, "UTF-8");
180
 
450 rajveer 181
    		Media media = CreationUtils.getRawMedia(entityID).get(label);
242 naveen 182
    		CreationUtils.removeMedia(entityID, label);
450 rajveer 183
    		String contentLabel ="";
184
    		if(media.getType().compareTo("image") == 0){
185
    			contentLabel = media.getLabel() + "~!~" + media.getFileName();
186
    			log.info("Type is image :" + contentLabel);
187
    		}
188
    		if(media.getType().compareTo("youtube") == 0){
189
    			contentLabel = media.getLabel() + "~!~" + media.getLocation();
190
    			log.info("Type is youtube :" + contentLabel);
191
    		}
192
 
193
 
455 rajveer 194
    		Entity entity = CreationUtils.getEntity(Long.parseLong(this.entityID));
448 rajveer 195
			FreeformContent freeformContent = entity.getSlide(slideID).getFreeformContent(); 
450 rajveer 196
			freeformContent.removeMedia(media.getType(), contentLabel);
448 rajveer 197
			entity.getSlide(slideID).setFreeformContent(freeformContent);
198
			updateEntity(entity);
199
			//CreationUtils.storeEntity(entity);
200
 
242 naveen 201
		} catch (Exception e) {
202
			log.error(CreationUtils.getStackTrace(e));
203
			this.setErrorString(CreationUtils.getStackTrace(e));
204
			return "fatal";
205
		}
206
 
448 rajveer 207
		this.redirectURL = "media?entityID=" + this.getEntityID() + "&slideID=" + this.getSlideID();
242 naveen 208
 
209
		return "redirect";
210
    }
211
    /**
212
     * 
208 naveen 213
     * @return
214
     * @throws Exception 
215
     */
216
    public Collection<Media> getMedia() throws Exception {
217
    	Collection<Media> allMedia = null;
455 rajveer 218
    	Entity entity = CreationUtils.getEntity(Long.parseLong(getEntityID()));
219
    	if(entity != null){
220
	    	Slide slide = entity.getSlide(Long.parseLong(getSlideID()));
221
	    	if(slide != null && slide.getFreeformContent() != null){
222
	    		List<String> labels = slide.getFreeformContent().getAllLabels();
223
 
224
		    	if(labels != null){
225
 
226
		    		if(this.rawMedia == null) {
227
		        		this.rawMedia = this.getRawMedia();
228
		        	}
229
 
230
		    		if(this.rawMedia != null) {
231
		    			allMedia = new ArrayList<Media>();
232
		    			for(String label: labels){
233
		    				allMedia.add(this.rawMedia.get(label));
234
		    			}
235
		    		}
236
		    	}
237
	    	}
208 naveen 238
    	}
455 rajveer 239
	    return allMedia;
208 naveen 240
    }
241
	/**
242
	 * @param id the id to set
243
	 */
448 rajveer 244
	public void setSlideID(String id) {
245
		this.slideID = id;
246
		log.info("token 2:" + this.slideID);
247
 
248
//		StringTokenizer tokenizer = new StringTokenizer(id,"_");
249
//		log.info("id:" + id);
250
//		log.info("token 1:" + tokenizer.nextToken());
251
//		log.info("token 2:" + tokenizer.nextToken());
252
//		this.slideID = tokenizer.nextToken();
253
//		log.info("token 2:" + this.slideID);
254
	}
255
	/**
256
	 * @return the id
257
	 */
258
	public String getSlideID() {
259
		return slideID;
260
	}
261
	/**
262
	 * @param id the id to set
263
	 */
208 naveen 264
	public void setEntityID(String id) {
265
		this.entityID = id;
448 rajveer 266
		log.info("token 2:" + this.entityID);
267
 
268
//		StringTokenizer tokenizer = new StringTokenizer(id,"_");
269
//		this.entityID = tokenizer.nextToken();
270
//		log.info("id:" + id);
271
//		log.info("token 2:" + this.entityID);
272
//		log.info("token 2:" + tokenizer.nextToken());
273
//		this.slideID = tokenizer.nextToken();
274
//		log.info("token 3:" + this.slideID);
208 naveen 275
	}
276
	/**
277
	 * @return the id
278
	 */
279
	public String getEntityID() {
280
		return entityID;
281
	}
282
	/**
283
	 * @param label the label to set
284
	 */
285
	public void setLabel(String label) {
286
		this.label = label;
287
	}
288
 
289
	/**
290
	 * @return the label
291
	 */
292
	public String getLabel() {
293
		return label;
294
	}
295
 
296
	/**
297
	 * @param pic the pic to set
298
	 */
299
	public void setPic(File pic) {
300
		this.pic = pic;
301
	}
302
	/**
303
	 * @return the pic
304
	 */
305
	public File getPic() {
306
		return pic;
307
	}
308
	/**
309
	 * @param picContentType the picContentType to set
310
	 */
311
	public void setPicContentType(String picContentType) {
312
		this.picContentType = picContentType;
313
	}
314
	/**
315
	 * @return the picContentType
316
	 */
317
	public String getPicContentType() {
318
		return picContentType;
319
	}
320
	/**
321
	 * @param picFileName the picFileName to set
322
	 */
323
	public void setPicFileName(String picFileName) {
324
		this.picFileName = picFileName;
325
	}
326
	/**
327
	 * @return the picFileName
328
	 */
329
	public String getPicFileName() {
330
		return picFileName;
331
	}
332
	/**
333
	 * @param fileSystemPath the fileSystemPath to set
334
	 */
335
	public void setFileSystemPath(String fileSystemPath) {
336
		this.fileSystemPath = fileSystemPath;
337
	}
338
	/**
339
	 * @return the fileSystemPath
340
	 */
341
	public String getFileSystemPath() {
342
		return fileSystemPath;
343
	}
344
 
345
    /**
346
	 * @param youtubeURL the youtubeURL to set
347
	 */
348
	public void setYoutubeURL(String youtubeURL) {
349
		this.youtubeURL = youtubeURL;
350
	}
351
 
352
	/**
353
	 * @return the youtubeURL
354
	 */
355
	public String getYoutubeURL() {
356
		return youtubeURL;
357
	}
358
 
359
	public String getUrl() {
360
    	return this.redirectURL;
361
    }
362
 
363
	/**
364
	 * @param errorString the exceptionString to set
365
	 */
366
	public void setErrorString(String errorString) {
367
		this.errorString = errorString;
368
	}
369
 
370
	/**
371
	 * @return the exceptionString
372
	 */
373
	public String getErrorString() {
374
		return errorString;
375
	}
376
 
377
	/**
378
	 * 
379
	 * @param media
380
	 * @throws Exception
381
	 */
382
	private void addMedia(Media media) throws Exception {
383
		Map<String, Media> rawMedia = this.getRawMedia();
384
 
385
		if(rawMedia == null) {
386
			rawMedia = new HashMap<String, Media>();
387
		}
388
		rawMedia.put(media.getLabel(), media);
389
 
455 rajveer 390
		DBUtils.store(rawMedia, Utils.CONTENT_DB_PATH + "media" + File.separator + this.getEntityID() + 
208 naveen 391
				".ser");
392
	}
393
 
394
 
395
    /**
396
     * @throws Exception 
397
     * 
398
     */
210 naveen 399
    private Map<String, Media> getRawMedia() throws Exception {
208 naveen 400
    	if(this.rawMedia == null) {
210 naveen 401
    		this.rawMedia = CreationUtils.getRawMedia(
402
    				Long.parseLong(this.getEntityID()));
208 naveen 403
    	}
404
 
405
    	return this.rawMedia;
406
    }
407
 
408
	/**
409
	 * 
410
	 * @throws Exception
411
	 */
412
	private String storeFile() throws Exception {
455 rajveer 413
		String mediaDirPath = Utils.CONTENT_DB_PATH + "media" + File.separator + this.getEntityID();
208 naveen 414
		File mediaDir = new File(mediaDirPath);
415
		if(!mediaDir.exists()) {
416
			mediaDir.mkdir();
417
		}
418
 
419
		String mediaFileName = mediaDirPath + "/" + this.getPicFileName();
420
		File mediaFile = new File(mediaFileName);
421
		mediaFile.createNewFile();
422
 
423
		File uploadedPicFile = this.getPic();
424
 
425
		InputStream in = new FileInputStream(uploadedPicFile);
426
 
427
		// appending output stream
455 rajveer 428
		// @rajveer : replacing the existing file 
429
		OutputStream out = new FileOutputStream(mediaFile); 
208 naveen 430
 
431
		try {
432
			IOUtils.copy(in, out);
433
		}
434
		finally {
435
			IOUtils.closeQuietly(in);
436
			IOUtils.closeQuietly(out);
437
		}
438
 
439
		return mediaFileName;
440
	}
242 naveen 441
 
442
	/**
443
	 * @param id the id to set
444
	 */
445
	public void setId(String id) {
448 rajveer 446
//		StringTokenizer tokenizer = new StringTokenizer(id,"_");
447
//		log.info("id:" + id);
448
//		this.id = tokenizer.nextToken();
449
//		log.info("token 1:" + tokenizer.nextToken());
450
//		this.slideID = tokenizer.nextToken();
451
//		log.info("token 2:" + this.slideID);
452
		log.info("id:" + id);
242 naveen 453
		this.id = id;
454
	}
455
 
456
	/**
457
	 * @return the id
458
	 */
459
	public String getId() {
460
		return id;
461
	}
448 rajveer 462
 
463
 
464
    /**
465
     * 
466
     * @param entity
467
     * @throws Exception
468
     */
469
	private void updateEntity(Entity entity) throws Exception {		
470
		log.info("#### EntityController.updateEntity ####");
471
		EntityContainer entContainer = Catalog.getInstance().getEntityContainer();
472
 
473
		ContentValidator validator = new ContentValidator();
474
		if(!validator.validate(entity)) {
475
			CreationUtils.addToIncomplete(entity);
476
 
477
			// Delete from repository if incomplete
478
			entContainer.deleteEntity(entity);
479
		}
480
		else {
481
			CreationUtils.deleteFromIncomplete(entity);
482
 
483
			entContainer.updateEntity(entity);
484
		}
485
 
486
		CreationUtils.storeEntity(entity);
487
 
488
		this.updateRepository();
489
	}
490
 
491
	/**
492
	 * 
493
	 * @throws Exception
494
	 */
495
	private void updateRepository() throws Exception {	
496
		EntityContainer entContainer = Catalog.getInstance().getEntityContainer();
497
 
498
		Map<Long, Entity> entities = entContainer.getEntities();
499
		log.info("entities.size():" + entities.size());
500
 
501
		Map<Long, List<Entity>> entitiesByCategory = 
502
			entContainer.getEntitiesbyCategory();
503
		log.info("entitiesByCategory.size():" + entitiesByCategory.size());
504
 
505
		CreationUtils.rewriteRepository(entities, entitiesByCategory);
506
	}
507
 
208 naveen 508
}