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