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