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
 
6
import in.shop2020.creation.util.CreationUtils;
7
import in.shop2020.creation.util.Media;
8
import in.shop2020.util.DBUtils;
9
import in.shop2020.util.Utils;
10
 
11
import java.io.File;
12
import java.io.FileInputStream;
13
import java.io.FileOutputStream;
14
import java.io.InputStream;
15
import java.io.OutputStream;
242 naveen 16
import java.net.URLDecoder;
208 naveen 17
import java.util.Collection;
18
import java.util.HashMap;
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;
24
import org.apache.struts2.convention.annotation.Result;
25
import org.apache.struts2.convention.annotation.Results;
26
import org.apache.struts2.rest.DefaultHttpHeaders;
27
import org.apache.struts2.rest.HttpHeaders;
28
 
29
/**
30
 * @author naveen
31
 *
32
 */
33
@Results({
34
    @Result(name="success", type="redirectAction", 
35
    		params = {"actionName" , "media"}),
36
    @Result(name="redirect", location="${url}", type="redirect")
37
})
38
public class MediaController {
39
 
40
	/**
41
	 * 
42
	 */
43
	private static Log log = LogFactory.getLog(MediaController.class);
44
 
45
	/**
46
	 * 
47
	 */
242 naveen 48
	private String id;
208 naveen 49
	private String entityID;
50
	private String label;
51
	private File pic;
52
	private String picContentType;
53
	private String picFileName;
54
	private String fileSystemPath;
55
	private String redirectURL;
56
	private String errorString;
57
	private String youtubeURL;
58
 
59
	private Map<String, Media> rawMedia;
60
 
61
 
62
    // GET /media
63
    public HttpHeaders index() {
64
    	log.info("MediaController.index");
65
 
66
        return new DefaultHttpHeaders("index").disableCaching();
67
    }
68
 
69
    // GET /media
70
    public String create() {
71
    	log.info("MediaController.create");
72
 
73
    	try {
74
    		log.info("getPicFileName:" + this.getPicFileName());
75
    		log.info("getFileSystemPath:" + this.getFileSystemPath());
76
    		log.info("getPicContentType:" + this.getPicContentType());
77
 
78
    		boolean hasdata = false;
79
    		String location = "";
80
    		String type = "";
81
    		if(this.getPic() != null) {
82
    			type = "image";
83
    			location = this.storeFile();
84
    			hasdata = true;
85
    		} 
86
    		else if(this.getYoutubeURL() != null) {
87
    			type = "youtube";
88
    			location = this.getYoutubeURL();
89
    			hasdata = true;
90
    		}
91
 
92
    		log.info("location:" + location);
93
    		log.info("label:" + this.getLabel());
94
 
95
    		if(hasdata) {
96
    			Media media = new Media(this.getLabel(), type, location);
97
 
98
    			if(this.getPic() != null) {
99
	    			media.setFileName(this.getPicFileName());
100
	    			media.setContentType(this.getPicContentType());
101
    			}
102
    			else {
103
	    			media.setFileName("");
104
	    			media.setContentType("");
105
    			}
106
    			this.addMedia(media);
107
    		}
108
 
109
		} catch (Exception e) {
110
			log.error(CreationUtils.getStackTrace(e));
111
			this.setErrorString(CreationUtils.getStackTrace(e));
112
			return "fatal";
113
		}
114
 
115
		this.redirectURL = "media?entityID=" + this.getEntityID();
116
 
117
		return "redirect";
118
    }
242 naveen 119
 
120
    // DELETE /entity/1000001
208 naveen 121
    /**
122
     * 
242 naveen 123
     */
124
    public String destroy() {
125
    	log.info("MediaController.destroy");
126
    	try {
127
    		long entityID = Long.parseLong(this.getEntityID());
128
    		String label = this.getId();
129
    		log.info("label:" + label);
130
 
131
    		label = URLDecoder.decode(label, "UTF-8");
132
 
133
    		CreationUtils.removeMedia(entityID, label);
134
		} catch (Exception e) {
135
			log.error(CreationUtils.getStackTrace(e));
136
			this.setErrorString(CreationUtils.getStackTrace(e));
137
			return "fatal";
138
		}
139
 
140
		this.redirectURL = "media?entityID=" + this.getEntityID();
141
 
142
		return "redirect";
143
    }
144
    /**
145
     * 
208 naveen 146
     * @return
147
     * @throws Exception 
148
     */
149
    public Collection<Media> getMedia() throws Exception {
150
    	Collection<Media> allMedia = null;
151
    	if(this.rawMedia == null) {
152
    		this.rawMedia = this.getRawMedia();
153
 
154
    		if(this.rawMedia != null) {
155
    			allMedia = rawMedia.values();
156
    		}
157
    	}
158
 
159
		return allMedia;
160
    }
161
	/**
162
	 * @param id the id to set
163
	 */
164
	public void setEntityID(String id) {
165
		this.entityID = id;
166
	}
167
	/**
168
	 * @return the id
169
	 */
170
	public String getEntityID() {
171
		return entityID;
172
	}
173
	/**
174
	 * @param label the label to set
175
	 */
176
	public void setLabel(String label) {
177
		this.label = label;
178
	}
179
 
180
	/**
181
	 * @return the label
182
	 */
183
	public String getLabel() {
184
		return label;
185
	}
186
 
187
	/**
188
	 * @param pic the pic to set
189
	 */
190
	public void setPic(File pic) {
191
		this.pic = pic;
192
	}
193
	/**
194
	 * @return the pic
195
	 */
196
	public File getPic() {
197
		return pic;
198
	}
199
	/**
200
	 * @param picContentType the picContentType to set
201
	 */
202
	public void setPicContentType(String picContentType) {
203
		this.picContentType = picContentType;
204
	}
205
	/**
206
	 * @return the picContentType
207
	 */
208
	public String getPicContentType() {
209
		return picContentType;
210
	}
211
	/**
212
	 * @param picFileName the picFileName to set
213
	 */
214
	public void setPicFileName(String picFileName) {
215
		this.picFileName = picFileName;
216
	}
217
	/**
218
	 * @return the picFileName
219
	 */
220
	public String getPicFileName() {
221
		return picFileName;
222
	}
223
	/**
224
	 * @param fileSystemPath the fileSystemPath to set
225
	 */
226
	public void setFileSystemPath(String fileSystemPath) {
227
		this.fileSystemPath = fileSystemPath;
228
	}
229
	/**
230
	 * @return the fileSystemPath
231
	 */
232
	public String getFileSystemPath() {
233
		return fileSystemPath;
234
	}
235
 
236
    /**
237
	 * @param youtubeURL the youtubeURL to set
238
	 */
239
	public void setYoutubeURL(String youtubeURL) {
240
		this.youtubeURL = youtubeURL;
241
	}
242
 
243
	/**
244
	 * @return the youtubeURL
245
	 */
246
	public String getYoutubeURL() {
247
		return youtubeURL;
248
	}
249
 
250
	public String getUrl() {
251
    	return this.redirectURL;
252
    }
253
 
254
	/**
255
	 * @param errorString the exceptionString to set
256
	 */
257
	public void setErrorString(String errorString) {
258
		this.errorString = errorString;
259
	}
260
 
261
	/**
262
	 * @return the exceptionString
263
	 */
264
	public String getErrorString() {
265
		return errorString;
266
	}
267
 
268
	/**
269
	 * 
270
	 * @param media
271
	 * @throws Exception
272
	 */
273
	private void addMedia(Media media) throws Exception {
274
		Map<String, Media> rawMedia = this.getRawMedia();
275
 
276
		if(rawMedia == null) {
277
			rawMedia = new HashMap<String, Media>();
278
		}
279
		rawMedia.put(media.getLabel(), media);
280
 
281
		DBUtils.store(rawMedia, Utils.MEDIA_DB_PATH + this.getEntityID() + 
282
				".ser");
283
	}
284
 
285
 
286
    /**
287
     * @throws Exception 
288
     * 
289
     */
210 naveen 290
    private Map<String, Media> getRawMedia() throws Exception {
208 naveen 291
    	if(this.rawMedia == null) {
210 naveen 292
    		this.rawMedia = CreationUtils.getRawMedia(
293
    				Long.parseLong(this.getEntityID()));
208 naveen 294
    	}
295
 
296
    	return this.rawMedia;
297
    }
298
 
299
	/**
300
	 * 
301
	 * @throws Exception
302
	 */
303
	private String storeFile() throws Exception {
304
		String mediaDirPath = Utils.MEDIA_DB_PATH + this.getEntityID();
305
		File mediaDir = new File(mediaDirPath);
306
		if(!mediaDir.exists()) {
307
			mediaDir.mkdir();
308
		}
309
 
310
		String mediaFileName = mediaDirPath + "/" + this.getPicFileName();
311
		File mediaFile = new File(mediaFileName);
312
		mediaFile.createNewFile();
313
 
314
		File uploadedPicFile = this.getPic();
315
 
316
		InputStream in = new FileInputStream(uploadedPicFile);
317
 
318
		// appending output stream
319
		OutputStream out = new FileOutputStream(mediaFile, true); 
320
 
321
		try {
322
			IOUtils.copy(in, out);
323
		}
324
		finally {
325
			IOUtils.closeQuietly(in);
326
			IOUtils.closeQuietly(out);
327
		}
328
 
329
		return mediaFileName;
330
	}
242 naveen 331
 
332
	/**
333
	 * @param id the id to set
334
	 */
335
	public void setId(String id) {
336
		this.id = id;
337
	}
338
 
339
	/**
340
	 * @return the id
341
	 */
342
	public String getId() {
343
		return id;
344
	}
208 naveen 345
}