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