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);
119
    			EntityContainer enContainer = Catalog.getInstance().getEntityContainer();
120
    			Entity entity = enContainer.getEntity(Long.parseLong(this.entityID));
121
    			FreeformContent freeformContent = entity.getSlide(Long.parseLong(this.slideID)).getFreeformContent();
122
    			freeformContent.addMedia(media.getType(), media.getLabel());
123
    			entity.getSlide(Long.parseLong(slideID)).setFreeformContent(freeformContent);
124
    			updateEntity(entity);
125
    			//CreationUtils.storeEntity(entity);
208 naveen 126
    		}
127
 
128
		} catch (Exception e) {
129
			log.error(CreationUtils.getStackTrace(e));
130
			this.setErrorString(CreationUtils.getStackTrace(e));
131
			return "fatal";
132
		}
133
 
448 rajveer 134
		this.redirectURL = "media?entityID=" + this.getEntityID() + "&slideID=" + this.getSlideID();;
208 naveen 135
 
136
		return "redirect";
137
    }
242 naveen 138
 
139
    // DELETE /entity/1000001
208 naveen 140
    /**
141
     * 
242 naveen 142
     */
143
    public String destroy() {
144
    	log.info("MediaController.destroy");
145
    	try {
146
    		long entityID = Long.parseLong(this.getEntityID());
448 rajveer 147
    		long slideID = Long.parseLong(this.getSlideID());
242 naveen 148
    		String label = this.getId();
149
    		log.info("label:" + label);
448 rajveer 150
    		log.info("slide:" + label);
151
    		log.info("entity:" + label);
242 naveen 152
 
153
    		label = URLDecoder.decode(label, "UTF-8");
154
 
155
    		CreationUtils.removeMedia(entityID, label);
448 rajveer 156
 
157
    		Entity entity = Catalog.getInstance().getEntityContainer().getEntity(entityID);
158
			FreeformContent freeformContent = entity.getSlide(slideID).getFreeformContent(); 
159
			freeformContent.removeMedia("youtube",label);
160
			freeformContent.removeMedia("image",label);
161
			entity.getSlide(slideID).setFreeformContent(freeformContent);
162
			updateEntity(entity);
163
			//CreationUtils.storeEntity(entity);
164
 
242 naveen 165
		} catch (Exception e) {
166
			log.error(CreationUtils.getStackTrace(e));
167
			this.setErrorString(CreationUtils.getStackTrace(e));
168
			return "fatal";
169
		}
170
 
448 rajveer 171
		this.redirectURL = "media?entityID=" + this.getEntityID() + "&slideID=" + this.getSlideID();
242 naveen 172
 
173
		return "redirect";
174
    }
175
    /**
176
     * 
208 naveen 177
     * @return
178
     * @throws Exception 
179
     */
180
    public Collection<Media> getMedia() throws Exception {
181
    	Collection<Media> allMedia = null;
182
    	if(this.rawMedia == null) {
183
    		this.rawMedia = this.getRawMedia();
184
 
185
    		if(this.rawMedia != null) {
186
    			allMedia = rawMedia.values();
187
    		}
188
    	}
189
 
190
		return allMedia;
191
    }
192
	/**
193
	 * @param id the id to set
194
	 */
448 rajveer 195
	public void setSlideID(String id) {
196
		this.slideID = id;
197
		log.info("token 2:" + this.slideID);
198
 
199
//		StringTokenizer tokenizer = new StringTokenizer(id,"_");
200
//		log.info("id:" + id);
201
//		log.info("token 1:" + tokenizer.nextToken());
202
//		log.info("token 2:" + tokenizer.nextToken());
203
//		this.slideID = tokenizer.nextToken();
204
//		log.info("token 2:" + this.slideID);
205
	}
206
	/**
207
	 * @return the id
208
	 */
209
	public String getSlideID() {
210
		return slideID;
211
	}
212
	/**
213
	 * @param id the id to set
214
	 */
208 naveen 215
	public void setEntityID(String id) {
216
		this.entityID = id;
448 rajveer 217
		log.info("token 2:" + this.entityID);
218
 
219
//		StringTokenizer tokenizer = new StringTokenizer(id,"_");
220
//		this.entityID = tokenizer.nextToken();
221
//		log.info("id:" + id);
222
//		log.info("token 2:" + this.entityID);
223
//		log.info("token 2:" + tokenizer.nextToken());
224
//		this.slideID = tokenizer.nextToken();
225
//		log.info("token 3:" + this.slideID);
208 naveen 226
	}
227
	/**
228
	 * @return the id
229
	 */
230
	public String getEntityID() {
231
		return entityID;
232
	}
233
	/**
234
	 * @param label the label to set
235
	 */
236
	public void setLabel(String label) {
237
		this.label = label;
238
	}
239
 
240
	/**
241
	 * @return the label
242
	 */
243
	public String getLabel() {
244
		return label;
245
	}
246
 
247
	/**
248
	 * @param pic the pic to set
249
	 */
250
	public void setPic(File pic) {
251
		this.pic = pic;
252
	}
253
	/**
254
	 * @return the pic
255
	 */
256
	public File getPic() {
257
		return pic;
258
	}
259
	/**
260
	 * @param picContentType the picContentType to set
261
	 */
262
	public void setPicContentType(String picContentType) {
263
		this.picContentType = picContentType;
264
	}
265
	/**
266
	 * @return the picContentType
267
	 */
268
	public String getPicContentType() {
269
		return picContentType;
270
	}
271
	/**
272
	 * @param picFileName the picFileName to set
273
	 */
274
	public void setPicFileName(String picFileName) {
275
		this.picFileName = picFileName;
276
	}
277
	/**
278
	 * @return the picFileName
279
	 */
280
	public String getPicFileName() {
281
		return picFileName;
282
	}
283
	/**
284
	 * @param fileSystemPath the fileSystemPath to set
285
	 */
286
	public void setFileSystemPath(String fileSystemPath) {
287
		this.fileSystemPath = fileSystemPath;
288
	}
289
	/**
290
	 * @return the fileSystemPath
291
	 */
292
	public String getFileSystemPath() {
293
		return fileSystemPath;
294
	}
295
 
296
    /**
297
	 * @param youtubeURL the youtubeURL to set
298
	 */
299
	public void setYoutubeURL(String youtubeURL) {
300
		this.youtubeURL = youtubeURL;
301
	}
302
 
303
	/**
304
	 * @return the youtubeURL
305
	 */
306
	public String getYoutubeURL() {
307
		return youtubeURL;
308
	}
309
 
310
	public String getUrl() {
311
    	return this.redirectURL;
312
    }
313
 
314
	/**
315
	 * @param errorString the exceptionString to set
316
	 */
317
	public void setErrorString(String errorString) {
318
		this.errorString = errorString;
319
	}
320
 
321
	/**
322
	 * @return the exceptionString
323
	 */
324
	public String getErrorString() {
325
		return errorString;
326
	}
327
 
328
	/**
329
	 * 
330
	 * @param media
331
	 * @throws Exception
332
	 */
333
	private void addMedia(Media media) throws Exception {
334
		Map<String, Media> rawMedia = this.getRawMedia();
335
 
336
		if(rawMedia == null) {
337
			rawMedia = new HashMap<String, Media>();
338
		}
339
		rawMedia.put(media.getLabel(), media);
340
 
341
		DBUtils.store(rawMedia, Utils.MEDIA_DB_PATH + this.getEntityID() + 
342
				".ser");
343
	}
344
 
345
 
346
    /**
347
     * @throws Exception 
348
     * 
349
     */
210 naveen 350
    private Map<String, Media> getRawMedia() throws Exception {
208 naveen 351
    	if(this.rawMedia == null) {
210 naveen 352
    		this.rawMedia = CreationUtils.getRawMedia(
353
    				Long.parseLong(this.getEntityID()));
208 naveen 354
    	}
355
 
356
    	return this.rawMedia;
357
    }
358
 
359
	/**
360
	 * 
361
	 * @throws Exception
362
	 */
363
	private String storeFile() throws Exception {
364
		String mediaDirPath = Utils.MEDIA_DB_PATH + this.getEntityID();
365
		File mediaDir = new File(mediaDirPath);
366
		if(!mediaDir.exists()) {
367
			mediaDir.mkdir();
368
		}
369
 
370
		String mediaFileName = mediaDirPath + "/" + this.getPicFileName();
371
		File mediaFile = new File(mediaFileName);
372
		mediaFile.createNewFile();
373
 
374
		File uploadedPicFile = this.getPic();
375
 
376
		InputStream in = new FileInputStream(uploadedPicFile);
377
 
378
		// appending output stream
379
		OutputStream out = new FileOutputStream(mediaFile, true); 
380
 
381
		try {
382
			IOUtils.copy(in, out);
383
		}
384
		finally {
385
			IOUtils.closeQuietly(in);
386
			IOUtils.closeQuietly(out);
387
		}
388
 
389
		return mediaFileName;
390
	}
242 naveen 391
 
392
	/**
393
	 * @param id the id to set
394
	 */
395
	public void setId(String id) {
448 rajveer 396
//		StringTokenizer tokenizer = new StringTokenizer(id,"_");
397
//		log.info("id:" + id);
398
//		this.id = tokenizer.nextToken();
399
//		log.info("token 1:" + tokenizer.nextToken());
400
//		this.slideID = tokenizer.nextToken();
401
//		log.info("token 2:" + this.slideID);
402
		log.info("id:" + id);
242 naveen 403
		this.id = id;
404
	}
405
 
406
	/**
407
	 * @return the id
408
	 */
409
	public String getId() {
410
		return id;
411
	}
448 rajveer 412
 
413
 
414
    /**
415
     * 
416
     * @param entity
417
     * @throws Exception
418
     */
419
	private void updateEntity(Entity entity) throws Exception {		
420
		log.info("#### EntityController.updateEntity ####");
421
		EntityContainer entContainer = Catalog.getInstance().getEntityContainer();
422
 
423
		ContentValidator validator = new ContentValidator();
424
		if(!validator.validate(entity)) {
425
			CreationUtils.addToIncomplete(entity);
426
 
427
			// Delete from repository if incomplete
428
			entContainer.deleteEntity(entity);
429
		}
430
		else {
431
			CreationUtils.deleteFromIncomplete(entity);
432
 
433
			entContainer.updateEntity(entity);
434
		}
435
 
436
		CreationUtils.storeEntity(entity);
437
 
438
		this.updateRepository();
439
	}
440
 
441
	/**
442
	 * 
443
	 * @throws Exception
444
	 */
445
	private void updateRepository() throws Exception {	
446
		EntityContainer entContainer = Catalog.getInstance().getEntityContainer();
447
 
448
		Map<Long, Entity> entities = entContainer.getEntities();
449
		log.info("entities.size():" + entities.size());
450
 
451
		Map<Long, List<Entity>> entitiesByCategory = 
452
			entContainer.getEntitiesbyCategory();
453
		log.info("entitiesByCategory.size():" + entitiesByCategory.size());
454
 
455
		CreationUtils.rewriteRepository(entities, entitiesByCategory);
456
	}
457
 
208 naveen 458
}