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;
16
import java.util.Collection;
17
import java.util.HashMap;
18
import java.util.Map;
19
 
20
import org.apache.commons.io.IOUtils;
21
import org.apache.juli.logging.Log;
22
import org.apache.juli.logging.LogFactory;
23
import org.apache.struts2.convention.annotation.Result;
24
import org.apache.struts2.convention.annotation.Results;
25
import org.apache.struts2.rest.DefaultHttpHeaders;
26
import org.apache.struts2.rest.HttpHeaders;
27
 
28
/**
29
 * @author naveen
30
 *
31
 */
32
@Results({
33
    @Result(name="success", type="redirectAction", 
34
    		params = {"actionName" , "media"}),
35
    @Result(name="redirect", location="${url}", type="redirect")
36
})
37
public class MediaController {
38
 
39
	/**
40
	 * 
41
	 */
42
	private static Log log = LogFactory.getLog(MediaController.class);
43
 
44
	/**
45
	 * 
46
	 */
47
	private String entityID;
48
	private String label;
49
	private File pic;
50
	private String picContentType;
51
	private String picFileName;
52
	private String fileSystemPath;
53
	private String redirectURL;
54
	private String errorString;
55
	private String youtubeURL;
56
 
57
	private Map<String, Media> rawMedia;
58
 
59
 
60
    // GET /media
61
    public HttpHeaders index() {
62
    	log.info("MediaController.index");
63
 
64
    	// TODO
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
    }
119
 
120
    /**
121
     * 
122
     * @return
123
     * @throws Exception 
124
     */
125
    public Collection<Media> getMedia() throws Exception {
126
    	Collection<Media> allMedia = null;
127
    	if(this.rawMedia == null) {
128
    		this.rawMedia = this.getRawMedia();
129
 
130
    		if(this.rawMedia != null) {
131
    			allMedia = rawMedia.values();
132
    		}
133
    	}
134
 
135
		return allMedia;
136
    }
137
	/**
138
	 * @param id the id to set
139
	 */
140
	public void setEntityID(String id) {
141
		this.entityID = id;
142
	}
143
	/**
144
	 * @return the id
145
	 */
146
	public String getEntityID() {
147
		return entityID;
148
	}
149
	/**
150
	 * @param label the label to set
151
	 */
152
	public void setLabel(String label) {
153
		this.label = label;
154
	}
155
 
156
	/**
157
	 * @return the label
158
	 */
159
	public String getLabel() {
160
		return label;
161
	}
162
 
163
	/**
164
	 * @param pic the pic to set
165
	 */
166
	public void setPic(File pic) {
167
		this.pic = pic;
168
	}
169
	/**
170
	 * @return the pic
171
	 */
172
	public File getPic() {
173
		return pic;
174
	}
175
	/**
176
	 * @param picContentType the picContentType to set
177
	 */
178
	public void setPicContentType(String picContentType) {
179
		this.picContentType = picContentType;
180
	}
181
	/**
182
	 * @return the picContentType
183
	 */
184
	public String getPicContentType() {
185
		return picContentType;
186
	}
187
	/**
188
	 * @param picFileName the picFileName to set
189
	 */
190
	public void setPicFileName(String picFileName) {
191
		this.picFileName = picFileName;
192
	}
193
	/**
194
	 * @return the picFileName
195
	 */
196
	public String getPicFileName() {
197
		return picFileName;
198
	}
199
	/**
200
	 * @param fileSystemPath the fileSystemPath to set
201
	 */
202
	public void setFileSystemPath(String fileSystemPath) {
203
		this.fileSystemPath = fileSystemPath;
204
	}
205
	/**
206
	 * @return the fileSystemPath
207
	 */
208
	public String getFileSystemPath() {
209
		return fileSystemPath;
210
	}
211
 
212
    /**
213
	 * @param youtubeURL the youtubeURL to set
214
	 */
215
	public void setYoutubeURL(String youtubeURL) {
216
		this.youtubeURL = youtubeURL;
217
	}
218
 
219
	/**
220
	 * @return the youtubeURL
221
	 */
222
	public String getYoutubeURL() {
223
		return youtubeURL;
224
	}
225
 
226
	public String getUrl() {
227
    	return this.redirectURL;
228
    }
229
 
230
	/**
231
	 * @param errorString the exceptionString to set
232
	 */
233
	public void setErrorString(String errorString) {
234
		this.errorString = errorString;
235
	}
236
 
237
	/**
238
	 * @return the exceptionString
239
	 */
240
	public String getErrorString() {
241
		return errorString;
242
	}
243
 
244
	/**
245
	 * 
246
	 * @param media
247
	 * @throws Exception
248
	 */
249
	private void addMedia(Media media) throws Exception {
250
		Map<String, Media> rawMedia = this.getRawMedia();
251
 
252
		if(rawMedia == null) {
253
			rawMedia = new HashMap<String, Media>();
254
		}
255
		rawMedia.put(media.getLabel(), media);
256
 
257
		DBUtils.store(rawMedia, Utils.MEDIA_DB_PATH + this.getEntityID() + 
258
				".ser");
259
	}
260
 
261
 
262
    /**
263
     * @throws Exception 
264
     * 
265
     */
210 naveen 266
    private Map<String, Media> getRawMedia() throws Exception {
208 naveen 267
    	if(this.rawMedia == null) {
210 naveen 268
    		this.rawMedia = CreationUtils.getRawMedia(
269
    				Long.parseLong(this.getEntityID()));
208 naveen 270
    	}
271
 
272
    	return this.rawMedia;
273
    }
274
 
275
	/**
276
	 * 
277
	 * @throws Exception
278
	 */
279
	private String storeFile() throws Exception {
280
		String mediaDirPath = Utils.MEDIA_DB_PATH + this.getEntityID();
281
		File mediaDir = new File(mediaDirPath);
282
		if(!mediaDir.exists()) {
283
			mediaDir.mkdir();
284
		}
285
 
286
		String mediaFileName = mediaDirPath + "/" + this.getPicFileName();
287
		File mediaFile = new File(mediaFileName);
288
		mediaFile.createNewFile();
289
 
290
		File uploadedPicFile = this.getPic();
291
 
292
		InputStream in = new FileInputStream(uploadedPicFile);
293
 
294
		// appending output stream
295
		OutputStream out = new FileOutputStream(mediaFile, true); 
296
 
297
		try {
298
			IOUtils.copy(in, out);
299
		}
300
		finally {
301
			IOUtils.closeQuietly(in);
302
			IOUtils.closeQuietly(out);
303
		}
304
 
305
		return mediaFileName;
306
	}
307
}