Subversion Repositories SmartDukaan

Rev

Rev 1378 | Rev 5380 | 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
 
448 rajveer 6
import in.shop2020.metamodel.core.Entity;
7
import in.shop2020.metamodel.core.FreeformContent;
2720 mandeep.dh 8
import in.shop2020.metamodel.core.Media;
9
import in.shop2020.metamodel.core.Media.Type;
448 rajveer 10
import in.shop2020.metamodel.core.Slide;
2720 mandeep.dh 11
import in.shop2020.metamodel.util.CreationUtils;
208 naveen 12
import in.shop2020.util.Utils;
13
 
14
import java.io.File;
15
import java.io.FileInputStream;
16
import java.io.FileOutputStream;
17
import java.io.InputStream;
18
import java.io.OutputStream;
242 naveen 19
import java.net.URLDecoder;
208 naveen 20
import java.util.Map;
21
 
22
import org.apache.commons.io.IOUtils;
23
import org.apache.juli.logging.Log;
24
import org.apache.juli.logging.LogFactory;
1051 rajveer 25
import org.apache.struts2.convention.annotation.InterceptorRef;
26
import org.apache.struts2.convention.annotation.InterceptorRefs;
208 naveen 27
import org.apache.struts2.convention.annotation.Result;
28
import org.apache.struts2.convention.annotation.Results;
29
import org.apache.struts2.rest.DefaultHttpHeaders;
30
import org.apache.struts2.rest.HttpHeaders;
31
 
1051 rajveer 32
@InterceptorRefs({
33
    @InterceptorRef("myDefault"),
34
    @InterceptorRef("login")
35
})
36
 
208 naveen 37
/**
38
 * @author naveen
39
 *
40
 */
41
@Results({
42
    @Result(name="success", type="redirectAction", 
43
    		params = {"actionName" , "media"}),
44
    @Result(name="redirect", location="${url}", type="redirect")
45
})
1051 rajveer 46
public class MediaController extends BaseController {
208 naveen 47
 
48
	/**
2720 mandeep.dh 49
     * 
50
     */
51
    private static final long serialVersionUID = 4482901443477816033L;
52
 
53
    /**
208 naveen 54
	 * 
55
	 */
56
	private static Log log = LogFactory.getLog(MediaController.class);
57
 
58
	/**
59
	 * 
60
	 */
242 naveen 61
	private String id;
208 naveen 62
	private String entityID;
448 rajveer 63
	private String slideID;
208 naveen 64
	private String label;
480 rajveer 65
	private String title;
2720 mandeep.dh 66
	private File   upload;
67
	private String uploadFileName;
68
    private String location;
208 naveen 69
	private String fileSystemPath;
70
	private String redirectURL;
71
	private String errorString;
2720 mandeep.dh 72
	private String mediaType;
73
 
208 naveen 74
    // GET /media
75
    public HttpHeaders index() {
76
    	log.info("MediaController.index");
77
 
78
        return new DefaultHttpHeaders("index").disableCaching();
79
    }
80
 
81
    // GET /media
82
    public String create() {
83
    	log.info("MediaController.create");
84
 
85
    	try {
2720 mandeep.dh 86
    		log.info("Location:" + location);
87
    		log.info("UploadFileName:" + uploadFileName);
88
    		log.info("Label:" + label);
89
    		log.info("mediaType:" + mediaType);
208 naveen 90
    		log.info("getFileSystemPath:" + this.getFileSystemPath());
2720 mandeep.dh 91
 
208 naveen 92
    		boolean hasdata = false;
93
    		String location = "";
2720 mandeep.dh 94
    		if(this.getUpload() != null) {
208 naveen 95
    			location = this.storeFile();
96
    			hasdata = true;
2720 mandeep.dh 97
    		}
98
    		else if(this.getLocation() != null) {
99
    			location = this.getLocation();
208 naveen 100
    			hasdata = true;
101
    		}
2720 mandeep.dh 102
 
208 naveen 103
    		log.info("location:" + location);
104
    		log.info("label:" + this.getLabel());
480 rajveer 105
    		log.info("getTitle:" + this.getTitle());
208 naveen 106
 
107
    		if(hasdata) {
2720 mandeep.dh 108
    			Media media = new Media(this.getLabel(), Media.Type.valueOf(mediaType), location);
480 rajveer 109
    			media.setTitle(this.getTitle());
2720 mandeep.dh 110
    			media.setFileName(uploadFileName);
111
 
448 rajveer 112
    			log.info("entity ID:" + this.entityID);
113
    			log.info("slide ID:" + this.slideID);
455 rajveer 114
 
115
    			Entity entity = CreationUtils.getEntity(Long.parseLong(this.entityID));
116
 
117
	    		Slide slide = entity.getSlide(Long.parseLong(this.slideID));
118
    			if(slide == null){
119
    				slide = new Slide(Long.parseLong(this.getSlideID()));
120
    				entity.addSlide(slide);
121
    			}
122
    			FreeformContent freeformContent = slide.getFreeformContent();
123
    			if(freeformContent == null){
124
    				freeformContent = new FreeformContent();
125
    				slide.setFreeformContent(freeformContent);
126
    			}
1051 rajveer 127
    			freeformContent.addMedia(media);
448 rajveer 128
    			entity.getSlide(Long.parseLong(slideID)).setFreeformContent(freeformContent);
1051 rajveer 129
 
130
    			CreationUtils.updateEntity(entity);
208 naveen 131
    		}
132
 
133
		} catch (Exception e) {
134
			log.error(CreationUtils.getStackTrace(e));
135
			this.setErrorString(CreationUtils.getStackTrace(e));
136
			return "fatal";
137
		}
138
 
448 rajveer 139
		this.redirectURL = "media?entityID=" + this.getEntityID() + "&slideID=" + this.getSlideID();;
208 naveen 140
 
141
		return "redirect";
142
    }
242 naveen 143
 
144
    // DELETE /entity/1000001
208 naveen 145
    /**
146
     * 
242 naveen 147
     */
148
    public String destroy() {
149
    	log.info("MediaController.destroy");
150
    	try {
448 rajveer 151
    		long slideID = Long.parseLong(this.getSlideID());
242 naveen 152
 
1378 rajveer 153
    		String label = reqparams.get("label")[0];
154
 
242 naveen 155
    		label = URLDecoder.decode(label, "UTF-8");
1378 rajveer 156
    		System.out.println("Label is label:" + label);
242 naveen 157
 
455 rajveer 158
    		Entity entity = CreationUtils.getEntity(Long.parseLong(this.entityID));
1051 rajveer 159
 
448 rajveer 160
			FreeformContent freeformContent = entity.getSlide(slideID).getFreeformContent(); 
1051 rajveer 161
			freeformContent.removeMedia(label);
448 rajveer 162
			entity.getSlide(slideID).setFreeformContent(freeformContent);
163
 
1051 rajveer 164
			CreationUtils.updateEntity(entity);
165
 
242 naveen 166
		} catch (Exception e) {
167
			log.error(CreationUtils.getStackTrace(e));
168
			this.setErrorString(CreationUtils.getStackTrace(e));
169
			return "fatal";
170
		}
171
 
448 rajveer 172
		this.redirectURL = "media?entityID=" + this.getEntityID() + "&slideID=" + this.getSlideID();
242 naveen 173
 
174
		return "redirect";
175
    }
1051 rajveer 176
 
242 naveen 177
    /**
208 naveen 178
	 * @param id the id to set
179
	 */
448 rajveer 180
	public void setSlideID(String id) {
181
		this.slideID = id;
182
		log.info("token 2:" + this.slideID);
183
	}
184
	/**
185
	 * @return the id
186
	 */
187
	public String getSlideID() {
188
		return slideID;
189
	}
190
	/**
191
	 * @param id the id to set
192
	 */
208 naveen 193
	public void setEntityID(String id) {
194
		this.entityID = id;
448 rajveer 195
		log.info("token 2:" + this.entityID);
208 naveen 196
	}
197
 
198
	/**
2720 mandeep.dh 199
     * @return all media elements for the concerned slide
200
     *
201
     * @throws NumberFormatException
202
     * @throws Exception
203
     */
204
    public Map<String, Media> getAllMedia() throws NumberFormatException,
205
            Exception {
206
        Slide slide = CreationUtils.getEntity(Long.parseLong(this.entityID))
207
                .getSlide(Long.parseLong(this.slideID));
208
        if (slide != null) {
209
            FreeformContent ffc = slide.getFreeformContent();
210
            if (ffc != null) {
211
                return ffc.getMedias();
212
            }
213
        }
214
        return null;
215
    }
208 naveen 216
 
217
    /**
2720 mandeep.dh 218
     * Returns all different types of media
219
     *
220
     * @return
221
     */
222
    public Type[] getMediaTypes() {
223
        return Type.values();
208 naveen 224
    }
225
 
226
	/**
227
	 * 
228
	 * @throws Exception
229
	 */
230
	private String storeFile() throws Exception {
455 rajveer 231
		String mediaDirPath = Utils.CONTENT_DB_PATH + "media" + File.separator + this.getEntityID();
208 naveen 232
		File mediaDir = new File(mediaDirPath);
233
		if(!mediaDir.exists()) {
234
			mediaDir.mkdir();
235
		}
2720 mandeep.dh 236
 
237
		String mediaFileName = mediaDirPath + "/" + uploadFileName;
208 naveen 238
		File mediaFile = new File(mediaFileName);
239
		mediaFile.createNewFile();
240
 
2720 mandeep.dh 241
		File uploadedPicFile = this.getUpload();
208 naveen 242
 
243
		InputStream in = new FileInputStream(uploadedPicFile);
244
 
245
		// appending output stream
455 rajveer 246
		// @rajveer : replacing the existing file 
247
		OutputStream out = new FileOutputStream(mediaFile); 
208 naveen 248
 
249
		try {
250
			IOUtils.copy(in, out);
251
		}
252
		finally {
253
			IOUtils.closeQuietly(in);
254
			IOUtils.closeQuietly(out);
255
		}
256
 
257
		return mediaFileName;
258
	}
242 naveen 259
 
260
	/**
261
	 * @param id the id to set
262
	 */
263
	public void setId(String id) {
448 rajveer 264
		log.info("id:" + id);
242 naveen 265
		this.id = id;
266
	}
267
 
268
	/**
269
	 * @return the id
270
	 */
271
	public String getId() {
272
		return id;
273
	}
2720 mandeep.dh 274
 
275
	   /**
276
     * @return the id
277
     */
278
    public String getEntityID() {
279
        return entityID;
280
    }
281
    /**
282
     * @param label the label to set
283
     */
284
    public void setLabel(String label) {
285
        this.label = label;
286
    }
448 rajveer 287
 
2720 mandeep.dh 288
    /**
289
     * @return the label
290
     */
291
    public String getLabel() {
292
        return label;
293
    }
448 rajveer 294
 
2720 mandeep.dh 295
    public void setTitle(String title) {
296
        this.title = title;
297
    }
1051 rajveer 298
 
2720 mandeep.dh 299
    public String getTitle() {
300
        return title;
301
    }
302
 
303
    /**
304
     * @param File to set
305
     */
306
    public void setUpload(File upload) {
307
        this.upload = upload;
308
    }
309
    /**
310
     * @return the file
311
     */
312
    public File getUpload() {
313
        return upload;
314
    }
315
    /**
316
     * @param location the picFileName to set
317
     */
318
    public void setLocation(String location) {
319
        this.location = location;
320
    }
321
    /**
322
     * @return the picFileName
323
     */
324
    public String getLocation() {
325
        return location;
326
    }
327
    /**
328
     * @param fileSystemPath the fileSystemPath to set
329
     */
330
    public void setFileSystemPath(String fileSystemPath) {
331
        this.fileSystemPath = fileSystemPath;
332
    }
333
    /**
334
     * @return the fileSystemPath
335
     */
336
    public String getFileSystemPath() {
337
        return fileSystemPath;
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
 
358
    public String getMediaType() {
359
        return mediaType;
360
    }
361
 
362
    public void setMediaType(String mediaType) {
363
        this.mediaType = mediaType;
364
    }
365
 
366
    public String getUploadFileName() {
367
        return uploadFileName;
368
    }
369
 
370
    public void setUploadFileName(String uploadFileName) {
371
        this.uploadFileName = uploadFileName;
372
    }
373
 
374
}