Subversion Repositories SmartDukaan

Rev

Rev 1081 | Rev 2720 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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