Rev 5380 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/****/package in.shop2020.creation.controllers;import in.shop2020.metamodel.core.Entity;import in.shop2020.metamodel.core.FreeformContent;import in.shop2020.metamodel.core.Media;import in.shop2020.metamodel.core.Media.Type;import in.shop2020.metamodel.core.Slide;import in.shop2020.metamodel.util.CreationUtils;import in.shop2020.util.Utils;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.net.URLDecoder;import java.util.Map;import org.apache.commons.io.IOUtils;import org.apache.juli.logging.Log;import org.apache.juli.logging.LogFactory;import org.apache.struts2.convention.annotation.InterceptorRef;import org.apache.struts2.convention.annotation.InterceptorRefs;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import org.apache.struts2.rest.DefaultHttpHeaders;import org.apache.struts2.rest.HttpHeaders;@InterceptorRefs({@InterceptorRef("myDefault"),@InterceptorRef("login")})/*** @author naveen**/@Results({@Result(name="success", type="redirectAction",params = {"actionName" , "media"}),@Result(name="redirect", location="${url}", type="redirect")})public class MediaController extends BaseController {/****/private static final long serialVersionUID = 4482901443477816033L;/****/private static Log log = LogFactory.getLog(MediaController.class);/****/private String id;private String entityID;private String slideID;private String label;private String title;private File upload;private String uploadFileName;private String location;private String fileSystemPath;private String redirectURL;private String errorString;private String mediaType;// GET /mediapublic HttpHeaders index() {log.info("MediaController.index");return new DefaultHttpHeaders("index").disableCaching();}// GET /mediapublic String create() {log.info("MediaController.create");try {log.info("Location:" + location);log.info("UploadFileName:" + uploadFileName);log.info("Label:" + label);log.info("mediaType:" + mediaType);log.info("getFileSystemPath:" + this.getFileSystemPath());boolean hasdata = false;String location = "";if(this.getUpload() != null) {location = this.storeFile();hasdata = true;}else if(this.getLocation() != null) {location = this.getLocation();hasdata = true;}log.info("location:" + location);log.info("label:" + this.getLabel());log.info("getTitle:" + this.getTitle());if(hasdata) {Media media = new Media(this.getLabel(), Media.Type.valueOf(mediaType), location);media.setTitle(this.getTitle());media.setFileName(uploadFileName);log.info("entity ID:" + this.entityID);log.info("slide ID:" + this.slideID);Entity entity = CreationUtils.getEntity(Long.parseLong(this.entityID));Slide slide = entity.getSlide(Long.parseLong(this.slideID));if(slide == null){slide = new Slide(Long.parseLong(this.getSlideID()));entity.addSlide(slide);}FreeformContent freeformContent = slide.getFreeformContent();if(freeformContent == null){freeformContent = new FreeformContent();slide.setFreeformContent(freeformContent);}freeformContent.addMedia(media);entity.getSlide(Long.parseLong(slideID)).setFreeformContent(freeformContent);CreationUtils.updateEntity(entity);}} catch (Exception e) {log.error(CreationUtils.getStackTrace(e));this.setErrorString(CreationUtils.getStackTrace(e));return "fatal";}this.redirectURL = "media?entityID=" + this.getEntityID() + "&slideID=" + this.getSlideID();;return "redirect";}// DELETE /entity/1000001/****/public String destroy() {log.info("MediaController.destroy");try {long slideID = Long.parseLong(this.getSlideID());String label = reqparams.get("label")[0];//No need to decode as all URLs are decoded by the filters//label = URLDecoder.decode(label, "UTF-8");System.out.println("Label is label:" + label);Entity entity = CreationUtils.getEntity(Long.parseLong(this.entityID));FreeformContent freeformContent = entity.getSlide(slideID).getFreeformContent();freeformContent.removeMedia(label);entity.getSlide(slideID).setFreeformContent(freeformContent);CreationUtils.updateEntity(entity);} catch (Exception e) {log.error(CreationUtils.getStackTrace(e));this.setErrorString(CreationUtils.getStackTrace(e));return "fatal";}this.redirectURL = "media?entityID=" + this.getEntityID() + "&slideID=" + this.getSlideID();return "redirect";}/*** @param id the id to set*/public void setSlideID(String id) {this.slideID = id;log.info("token 2:" + this.slideID);}/*** @return the id*/public String getSlideID() {return slideID;}/*** @param id the id to set*/public void setEntityID(String id) {this.entityID = id;log.info("token 2:" + this.entityID);}/*** @return all media elements for the concerned slide** @throws NumberFormatException* @throws Exception*/public Map<String, Media> getAllMedia() throws NumberFormatException,Exception {Slide slide = CreationUtils.getEntity(Long.parseLong(this.entityID)).getSlide(Long.parseLong(this.slideID));if (slide != null) {FreeformContent ffc = slide.getFreeformContent();if (ffc != null) {return ffc.getMedias();}}return null;}/*** Returns all different types of media** @return*/public Type[] getMediaTypes() {return Type.values();}/**** @throws Exception*/private String storeFile() throws Exception {String mediaDirPath = Utils.CONTENT_DB_PATH + "media" + File.separator + this.getEntityID();File mediaDir = new File(mediaDirPath);if(!mediaDir.exists()) {mediaDir.mkdir();}String mediaFileName = mediaDirPath + "/" + uploadFileName;File mediaFile = new File(mediaFileName);mediaFile.createNewFile();File uploadedPicFile = this.getUpload();InputStream in = new FileInputStream(uploadedPicFile);// appending output stream// @rajveer : replacing the existing fileOutputStream out = new FileOutputStream(mediaFile);try {IOUtils.copy(in, out);}finally {IOUtils.closeQuietly(in);IOUtils.closeQuietly(out);}return mediaFileName;}/*** @param id the id to set*/public void setId(String id) {log.info("id:" + id);this.id = id;}/*** @return the id*/public String getId() {return id;}/*** @return the id*/public String getEntityID() {return entityID;}/*** @param label the label to set*/public void setLabel(String label) {this.label = label.trim();}/*** @return the label*/public String getLabel() {return label;}public void setTitle(String title) {this.title = title;}public String getTitle() {return title;}/*** @param File to set*/public void setUpload(File upload) {this.upload = upload;}/*** @return the file*/public File getUpload() {return upload;}/*** @param location the picFileName to set*/public void setLocation(String location) {this.location = location;}/*** @return the picFileName*/public String getLocation() {return location;}/*** @param fileSystemPath the fileSystemPath to set*/public void setFileSystemPath(String fileSystemPath) {this.fileSystemPath = fileSystemPath;}/*** @return the fileSystemPath*/public String getFileSystemPath() {return fileSystemPath;}public String getUrl() {return this.redirectURL;}/*** @param errorString the exceptionString to set*/public void setErrorString(String errorString) {this.errorString = errorString;}/*** @return the exceptionString*/public String getErrorString() {return errorString;}public String getMediaType() {return mediaType;}public void setMediaType(String mediaType) {this.mediaType = mediaType;}public String getUploadFileName() {return uploadFileName;}public void setUploadFileName(String uploadFileName) {this.uploadFileName = uploadFileName;}}