Subversion Repositories SmartDukaan

Rev

Rev 1051 | Rev 1378 | 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
    		String label = this.getId();
176
    		log.info("label:" + label);
448 rajveer 177
    		log.info("slide:" + label);
178
    		log.info("entity:" + label);
242 naveen 179
 
180
    		label = URLDecoder.decode(label, "UTF-8");
181
 
455 rajveer 182
    		Entity entity = CreationUtils.getEntity(Long.parseLong(this.entityID));
1051 rajveer 183
 
448 rajveer 184
			FreeformContent freeformContent = entity.getSlide(slideID).getFreeformContent(); 
1051 rajveer 185
			freeformContent.removeMedia(label);
448 rajveer 186
			entity.getSlide(slideID).setFreeformContent(freeformContent);
187
 
1051 rajveer 188
			CreationUtils.updateEntity(entity);
189
 
242 naveen 190
		} catch (Exception e) {
191
			log.error(CreationUtils.getStackTrace(e));
192
			this.setErrorString(CreationUtils.getStackTrace(e));
193
			return "fatal";
194
		}
195
 
448 rajveer 196
		this.redirectURL = "media?entityID=" + this.getEntityID() + "&slideID=" + this.getSlideID();
242 naveen 197
 
198
		return "redirect";
199
    }
1051 rajveer 200
 
242 naveen 201
    /**
208 naveen 202
	 * @param id the id to set
203
	 */
448 rajveer 204
	public void setSlideID(String id) {
205
		this.slideID = id;
206
		log.info("token 2:" + this.slideID);
207
 
208
//		StringTokenizer tokenizer = new StringTokenizer(id,"_");
209
//		log.info("id:" + id);
210
//		log.info("token 1:" + tokenizer.nextToken());
211
//		log.info("token 2:" + tokenizer.nextToken());
212
//		this.slideID = tokenizer.nextToken();
213
//		log.info("token 2:" + this.slideID);
214
	}
215
	/**
216
	 * @return the id
217
	 */
218
	public String getSlideID() {
219
		return slideID;
220
	}
221
	/**
222
	 * @param id the id to set
223
	 */
208 naveen 224
	public void setEntityID(String id) {
225
		this.entityID = id;
448 rajveer 226
		log.info("token 2:" + this.entityID);
227
 
228
//		StringTokenizer tokenizer = new StringTokenizer(id,"_");
229
//		this.entityID = tokenizer.nextToken();
230
//		log.info("id:" + id);
231
//		log.info("token 2:" + this.entityID);
232
//		log.info("token 2:" + tokenizer.nextToken());
233
//		this.slideID = tokenizer.nextToken();
234
//		log.info("token 3:" + this.slideID);
208 naveen 235
	}
236
	/**
237
	 * @return the id
238
	 */
239
	public String getEntityID() {
240
		return entityID;
241
	}
242
	/**
243
	 * @param label the label to set
244
	 */
245
	public void setLabel(String label) {
246
		this.label = label;
247
	}
248
 
249
	/**
250
	 * @return the label
251
	 */
252
	public String getLabel() {
253
		return label;
254
	}
255
 
480 rajveer 256
	public void setTitle(String title) {
257
		this.title = title;
258
	}
259
 
260
	public String getTitle() {
261
		return title;
262
	}
263
 
264
	public void setVideoType(String videoType) {
265
		this.videoType = videoType;
266
	}
267
 
268
	public String getVideoType() {
269
		return videoType;
270
	}
271
 
208 naveen 272
	/**
273
	 * @param pic the pic to set
274
	 */
275
	public void setPic(File pic) {
276
		this.pic = pic;
277
	}
278
	/**
279
	 * @return the pic
280
	 */
281
	public File getPic() {
282
		return pic;
283
	}
284
	/**
285
	 * @param picContentType the picContentType to set
286
	 */
287
	public void setPicContentType(String picContentType) {
288
		this.picContentType = picContentType;
289
	}
290
	/**
291
	 * @return the picContentType
292
	 */
293
	public String getPicContentType() {
294
		return picContentType;
295
	}
296
	/**
297
	 * @param picFileName the picFileName to set
298
	 */
299
	public void setPicFileName(String picFileName) {
300
		this.picFileName = picFileName;
301
	}
302
	/**
303
	 * @return the picFileName
304
	 */
305
	public String getPicFileName() {
306
		return picFileName;
307
	}
308
	/**
309
	 * @param fileSystemPath the fileSystemPath to set
310
	 */
311
	public void setFileSystemPath(String fileSystemPath) {
312
		this.fileSystemPath = fileSystemPath;
313
	}
314
	/**
315
	 * @return the fileSystemPath
316
	 */
317
	public String getFileSystemPath() {
318
		return fileSystemPath;
319
	}
320
 
321
    /**
322
	 * @param youtubeURL the youtubeURL to set
323
	 */
324
	public void setYoutubeURL(String youtubeURL) {
325
		this.youtubeURL = youtubeURL;
326
	}
327
 
328
	/**
329
	 * @return the youtubeURL
330
	 */
331
	public String getYoutubeURL() {
332
		return youtubeURL;
333
	}
334
 
335
	public String getUrl() {
336
    	return this.redirectURL;
337
    }
338
 
339
	/**
340
	 * @param errorString the exceptionString to set
341
	 */
342
	public void setErrorString(String errorString) {
343
		this.errorString = errorString;
344
	}
345
 
346
	/**
347
	 * @return the exceptionString
348
	 */
349
	public String getErrorString() {
350
		return errorString;
351
	}
352
 
1051 rajveer 353
 
208 naveen 354
 
355
	/**
356
	 * 
357
	 * @throws Exception
358
	 */
359
	private String storeFile() throws Exception {
455 rajveer 360
		String mediaDirPath = Utils.CONTENT_DB_PATH + "media" + File.separator + this.getEntityID();
208 naveen 361
		File mediaDir = new File(mediaDirPath);
362
		if(!mediaDir.exists()) {
363
			mediaDir.mkdir();
364
		}
365
 
366
		String mediaFileName = mediaDirPath + "/" + this.getPicFileName();
367
		File mediaFile = new File(mediaFileName);
368
		mediaFile.createNewFile();
369
 
370
		File uploadedPicFile = this.getPic();
371
 
372
		InputStream in = new FileInputStream(uploadedPicFile);
373
 
374
		// appending output stream
455 rajveer 375
		// @rajveer : replacing the existing file 
376
		OutputStream out = new FileOutputStream(mediaFile); 
208 naveen 377
 
378
		try {
379
			IOUtils.copy(in, out);
380
		}
381
		finally {
382
			IOUtils.closeQuietly(in);
383
			IOUtils.closeQuietly(out);
384
		}
385
 
386
		return mediaFileName;
387
	}
242 naveen 388
 
389
	/**
390
	 * @param id the id to set
391
	 */
392
	public void setId(String id) {
448 rajveer 393
//		StringTokenizer tokenizer = new StringTokenizer(id,"_");
394
//		log.info("id:" + id);
395
//		this.id = tokenizer.nextToken();
396
//		log.info("token 1:" + tokenizer.nextToken());
397
//		this.slideID = tokenizer.nextToken();
398
//		log.info("token 2:" + this.slideID);
399
		log.info("id:" + id);
242 naveen 400
		this.id = id;
401
	}
402
 
403
	/**
404
	 * @return the id
405
	 */
406
	public String getId() {
407
		return id;
408
	}
448 rajveer 409
 
1051 rajveer 410
    public Map<String,Media> getAllMedia() throws NumberFormatException, Exception {
411
    	 Slide slide =  CreationUtils.getEntity(Long.parseLong(this.entityID)).getSlide(Long.parseLong(this.slideID));
412
		 if(slide != null){
413
			 FreeformContent ffc = slide.getFreeformContent();
414
			 if(ffc != null){
415
				 return ffc.getMedias();
416
			 }
417
		 }
418
		return null;
448 rajveer 419
	}
420
 
1051 rajveer 421
 
208 naveen 422
}
1051 rajveer 423