Subversion Repositories SmartDukaan

Rev

Rev 480 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 480 Rev 1051
Line 1... Line 1...
1
/**
1
/**
2
 * 
2
 * 
3
 */
3
 */
4
package in.shop2020.creation.controllers;
4
package in.shop2020.creation.controllers;
5
 
5
 
6
import in.shop2020.creation.util.ContentValidator;
-
 
7
import in.shop2020.metamodel.util.CreationUtils;
6
import in.shop2020.metamodel.util.CreationUtils;
8
import in.shop2020.metamodel.core.Media;
7
import in.shop2020.metamodel.core.Media;
9
import in.shop2020.metamodel.core.Entity;
8
import in.shop2020.metamodel.core.Entity;
10
import in.shop2020.metamodel.core.FreeformContent;
9
import in.shop2020.metamodel.core.FreeformContent;
11
import in.shop2020.metamodel.core.Slide;
10
import in.shop2020.metamodel.core.Slide;
12
import in.shop2020.metamodel.definitions.Catalog;
-
 
13
import in.shop2020.metamodel.definitions.EntityContainer;
-
 
14
import in.shop2020.metamodel.util.CN;
-
 
15
import in.shop2020.metamodel.util.ExpandedEntity;
-
 
16
import in.shop2020.util.DBUtils;
-
 
17
import in.shop2020.util.Utils;
11
import in.shop2020.util.Utils;
18
 
12
 
19
import java.io.File;
13
import java.io.File;
20
import java.io.FileInputStream;
14
import java.io.FileInputStream;
21
import java.io.FileOutputStream;
15
import java.io.FileOutputStream;
22
import java.io.InputStream;
16
import java.io.InputStream;
23
import java.io.OutputStream;
17
import java.io.OutputStream;
24
import java.net.URLDecoder;
18
import java.net.URLDecoder;
25
import java.util.ArrayList;
-
 
26
import java.util.Collection;
-
 
27
import java.util.HashMap;
-
 
28
import java.util.List;
-
 
29
import java.util.Map;
19
import java.util.Map;
30
import java.util.StringTokenizer;
-
 
31
 
20
 
32
import org.apache.commons.io.IOUtils;
21
import org.apache.commons.io.IOUtils;
33
import org.apache.juli.logging.Log;
22
import org.apache.juli.logging.Log;
34
import org.apache.juli.logging.LogFactory;
23
import org.apache.juli.logging.LogFactory;
-
 
24
import org.apache.struts2.convention.annotation.InterceptorRef;
-
 
25
import org.apache.struts2.convention.annotation.InterceptorRefs;
35
import org.apache.struts2.convention.annotation.Result;
26
import org.apache.struts2.convention.annotation.Result;
36
import org.apache.struts2.convention.annotation.Results;
27
import org.apache.struts2.convention.annotation.Results;
37
import org.apache.struts2.rest.DefaultHttpHeaders;
28
import org.apache.struts2.rest.DefaultHttpHeaders;
38
import org.apache.struts2.rest.HttpHeaders;
29
import org.apache.struts2.rest.HttpHeaders;
-
 
30
 
-
 
31
@InterceptorRefs({
39
import org.apache.struts2.views.xslt.ArrayAdapter;
32
    @InterceptorRef("myDefault"),
-
 
33
    @InterceptorRef("login")
-
 
34
})
40
 
35
 
41
/**
36
/**
42
 * @author naveen
37
 * @author naveen
43
 *
38
 *
44
 */
39
 */
45
@Results({
40
@Results({
46
    @Result(name="success", type="redirectAction", 
41
    @Result(name="success", type="redirectAction", 
47
    		params = {"actionName" , "media"}),
42
    		params = {"actionName" , "media"}),
48
    @Result(name="redirect", location="${url}", type="redirect")
43
    @Result(name="redirect", location="${url}", type="redirect")
49
})
44
})
50
public class MediaController {
45
public class MediaController extends BaseController {
51
	
46
	
52
	/**
47
	/**
53
	 * 
48
	 * 
54
	 */
49
	 */
55
	private static Log log = LogFactory.getLog(MediaController.class);
50
	private static Log log = LogFactory.getLog(MediaController.class);
Line 68... Line 63...
68
	private String picFileName;
63
	private String picFileName;
69
	private String fileSystemPath;
64
	private String fileSystemPath;
70
	private String redirectURL;
65
	private String redirectURL;
71
	private String errorString;
66
	private String errorString;
72
	private String youtubeURL;
67
	private String youtubeURL;
73
 
-
 
74
	private Map<String, Media> rawMedia;
-
 
75
 
-
 
76
	
68
		
77
    // GET /media
69
    // GET /media
78
    public HttpHeaders index() {
70
    public HttpHeaders index() {
79
    	log.info("MediaController.index");
71
    	log.info("MediaController.index");
80
    	
72
    	
81
        return new DefaultHttpHeaders("index").disableCaching();
73
        return new DefaultHttpHeaders("index").disableCaching();
Line 120... Line 112...
120
    			else {
112
    			else {
121
	    			media.setFileName("");
113
	    			media.setFileName("");
122
	    			media.setContentType("");
114
	    			media.setContentType("");
123
	    			media.setVideoType(this.getVideoType());
115
	    			media.setVideoType(this.getVideoType());
124
    			}
116
    			}
125
    			this.addMedia(media);
117
    			//CreationUtils.addMedia(Long.parseLong(this.getEntityID()), media);
-
 
118
    			
126
    			log.info("entity ID:" + this.entityID);
119
    			log.info("entity ID:" + this.entityID);
127
    			log.info("slide ID:" + this.slideID);
120
    			log.info("slide ID:" + this.slideID);
128
    			
121
    			
129
    			/*
122
    			/*
130
    			String contentLabel = "";
123
    			String contentLabel = "";
Line 151... Line 144...
151
    			FreeformContent freeformContent = slide.getFreeformContent();
144
    			FreeformContent freeformContent = slide.getFreeformContent();
152
    			if(freeformContent == null){
145
    			if(freeformContent == null){
153
    				freeformContent = new FreeformContent();
146
    				freeformContent = new FreeformContent();
154
    				slide.setFreeformContent(freeformContent);
147
    				slide.setFreeformContent(freeformContent);
155
    			}
148
    			}
156
    			freeformContent.addMedia(media.getType(), media.getLabel());
149
    			freeformContent.addMedia(media);
157
    			entity.getSlide(Long.parseLong(slideID)).setFreeformContent(freeformContent);
150
    			entity.getSlide(Long.parseLong(slideID)).setFreeformContent(freeformContent);
158
    			updateEntity(entity);
151
    			
159
    			//CreationUtils.storeEntity(entity);
152
    			CreationUtils.updateEntity(entity);
160
    		}
153
    		}
161
 
154
 
162
		} catch (Exception e) {
155
		} catch (Exception e) {
163
			log.error(CreationUtils.getStackTrace(e));
156
			log.error(CreationUtils.getStackTrace(e));
164
			this.setErrorString(CreationUtils.getStackTrace(e));
157
			this.setErrorString(CreationUtils.getStackTrace(e));
Line 184... Line 177...
184
    		log.info("slide:" + label);
177
    		log.info("slide:" + label);
185
    		log.info("entity:" + label);
178
    		log.info("entity:" + label);
186
    		
179
    		
187
    		label = URLDecoder.decode(label, "UTF-8");
180
    		label = URLDecoder.decode(label, "UTF-8");
188
    		
181
    		
189
    		Media media = CreationUtils.getRawMedia(entityID).get(label);
-
 
190
    		CreationUtils.removeMedia(entityID, label);
-
 
191
    		/*
-
 
192
    		String contentLabel ="";
-
 
193
    		if(media.getType().compareTo("image") == 0){
-
 
194
    			contentLabel = media.getLabel() + "~!~" + media.getFileName();
-
 
195
    			log.info("Type is image :" + contentLabel);
-
 
196
    		}
-
 
197
    		if(media.getType().compareTo("youtube") == 0){
-
 
198
    			contentLabel = media.getLabel() + "~!~" + media.getLocation();
-
 
199
    			log.info("Type is youtube :" + contentLabel);
-
 
200
    		}
-
 
201
			*/
-
 
202
			
-
 
203
    		Entity entity = CreationUtils.getEntity(Long.parseLong(this.entityID));
182
    		Entity entity = CreationUtils.getEntity(Long.parseLong(this.entityID));
-
 
183
    		
204
			FreeformContent freeformContent = entity.getSlide(slideID).getFreeformContent(); 
184
			FreeformContent freeformContent = entity.getSlide(slideID).getFreeformContent(); 
205
			freeformContent.removeMedia(media.getType(), label);
185
			freeformContent.removeMedia(label);
206
			entity.getSlide(slideID).setFreeformContent(freeformContent);
186
			entity.getSlide(slideID).setFreeformContent(freeformContent);
207
			updateEntity(entity);
187
			
208
			//CreationUtils.storeEntity(entity);
188
			CreationUtils.updateEntity(entity);
209
			
189
			
210
		} catch (Exception e) {
190
		} catch (Exception e) {
211
			log.error(CreationUtils.getStackTrace(e));
191
			log.error(CreationUtils.getStackTrace(e));
212
			this.setErrorString(CreationUtils.getStackTrace(e));
192
			this.setErrorString(CreationUtils.getStackTrace(e));
213
			return "fatal";
193
			return "fatal";
Line 215... Line 195...
215
 
195
 
216
		this.redirectURL = "media?entityID=" + this.getEntityID() + "&slideID=" + this.getSlideID();
196
		this.redirectURL = "media?entityID=" + this.getEntityID() + "&slideID=" + this.getSlideID();
217
    	
197
    	
218
		return "redirect";
198
		return "redirect";
219
    }
199
    }
-
 
200
    
220
    /**
201
    /**
221
     * 
-
 
222
     * @return
-
 
223
     * @throws Exception 
-
 
224
     */
-
 
225
    public Collection<Media> getMedia() throws Exception {
-
 
226
    	Collection<Media> allMedia = null;
-
 
227
    	Entity entity = CreationUtils.getEntity(Long.parseLong(getEntityID()));
-
 
228
    	if(entity != null){
-
 
229
	    	Slide slide = entity.getSlide(Long.parseLong(getSlideID()));
-
 
230
	    	if(slide != null && slide.getFreeformContent() != null){
-
 
231
	    		List<String> labels = slide.getFreeformContent().getAllLabels();
-
 
232
		    	
-
 
233
		    	if(labels != null){
-
 
234
		    		
-
 
235
		    		if(this.rawMedia == null) {
-
 
236
		        		this.rawMedia = this.getRawMedia();
-
 
237
		        	}
-
 
238
		    	
-
 
239
		    		if(this.rawMedia != null) {
-
 
240
		    			allMedia = new ArrayList<Media>();
-
 
241
		    			for(String label: labels){
-
 
242
		    				allMedia.add(this.rawMedia.get(label));
-
 
243
		    			}
-
 
244
		    		}
-
 
245
		    	}
-
 
246
	    	}
-
 
247
    	}
-
 
248
	    return allMedia;
-
 
249
    }
-
 
250
	/**
-
 
251
	 * @param id the id to set
202
	 * @param id the id to set
252
	 */
203
	 */
253
	public void setSlideID(String id) {
204
	public void setSlideID(String id) {
254
		this.slideID = id;
205
		this.slideID = id;
255
		log.info("token 2:" + this.slideID);
206
		log.info("token 2:" + this.slideID);
Line 397... Line 348...
397
	 */
348
	 */
398
	public String getErrorString() {
349
	public String getErrorString() {
399
		return errorString;
350
		return errorString;
400
	}
351
	}
401
	
352
	
402
	/**
-
 
403
	 * 
-
 
404
	 * @param media
-
 
405
	 * @throws Exception
-
 
406
	 */
-
 
407
	private void addMedia(Media media) throws Exception {
-
 
408
		Map<String, Media> rawMedia = this.getRawMedia();
-
 
409
		
-
 
410
		if(rawMedia == null) {
-
 
411
			rawMedia = new HashMap<String, Media>();
-
 
412
		}
-
 
413
		rawMedia.put(media.getLabel(), media);
-
 
414
		
-
 
415
		DBUtils.store(rawMedia, Utils.CONTENT_DB_PATH + "media" + File.separator + this.getEntityID() + 
-
 
416
				".ser");
-
 
417
	}
-
 
418
 
-
 
419
    
353
	    
420
    /**
-
 
421
     * @throws Exception 
-
 
422
     * 
-
 
423
     */
-
 
424
    private Map<String, Media> getRawMedia() throws Exception {
-
 
425
    	if(this.rawMedia == null) {
-
 
426
    		this.rawMedia = CreationUtils.getRawMedia(
-
 
427
    				Long.parseLong(this.getEntityID()));
-
 
428
    	}
-
 
429
    	
-
 
430
    	return this.rawMedia;
-
 
431
    }
-
 
432
    
354
    
433
	/**
355
	/**
434
	 * 
356
	 * 
435
	 * @throws Exception
357
	 * @throws Exception
436
	 */
358
	 */
Line 482... Line 404...
482
	 * @return the id
404
	 * @return the id
483
	 */
405
	 */
484
	public String getId() {
406
	public String getId() {
485
		return id;
407
		return id;
486
	}
408
	}
487
	
-
 
488
	
-
 
489
    /**
-
 
490
     * 
-
 
491
     * @param entity
-
 
492
     * @throws Exception
-
 
493
     */
-
 
494
	private void updateEntity(Entity entity) throws Exception {		
-
 
495
		log.info("#### EntityController.updateEntity ####");
-
 
496
		EntityContainer entContainer = Catalog.getInstance().getEntityContainer();
-
 
497
		
-
 
498
		ContentValidator validator = new ContentValidator();
-
 
499
		if(!validator.validate(entity)) {
-
 
500
			CreationUtils.addToIncomplete(entity);
-
 
501
			
-
 
502
			// Delete from repository if incomplete
-
 
503
			entContainer.deleteEntity(entity);
-
 
504
		}
-
 
505
		else {
-
 
506
			CreationUtils.deleteFromIncomplete(entity);
-
 
507
			
-
 
508
			entContainer.updateEntity(entity);
-
 
509
		}
-
 
510
		
-
 
511
		CreationUtils.storeEntity(entity);
-
 
512
		
-
 
513
		this.updateRepository();
-
 
514
	}
-
 
515
 
409
 
516
	/**
-
 
517
	 * 
-
 
518
	 * @throws Exception
-
 
519
	 */
-
 
520
	private void updateRepository() throws Exception {	
410
    public Map<String,Media> getAllMedia() throws NumberFormatException, Exception {
521
		EntityContainer entContainer = Catalog.getInstance().getEntityContainer();
411
    	 Slide slide =  CreationUtils.getEntity(Long.parseLong(this.entityID)).getSlide(Long.parseLong(this.slideID));
522
		
412
		 if(slide != null){
523
		Map<Long, Entity> entities = entContainer.getEntities();
413
			 FreeformContent ffc = slide.getFreeformContent();
-
 
414
			 if(ffc != null){
524
		log.info("entities.size():" + entities.size());
415
				 return ffc.getMedias();
525
		
416
			 }
526
		Map<Long, List<Entity>> entitiesByCategory = 
-
 
527
			entContainer.getEntitiesbyCategory();
-
 
528
		log.info("entitiesByCategory.size():" + entitiesByCategory.size());
-
 
529
		
417
		 }
530
		CreationUtils.rewriteRepository(entities, entitiesByCategory);
418
		return null;
531
	}
419
	}
532
 
420
 
-
 
421
    
533
}
422
}
-
 
423