Subversion Repositories SmartDukaan

Rev

Rev 2768 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.creation.controllers;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import in.shop2020.metamodel.core.Helpdoc;
import in.shop2020.metamodel.util.CreationUtils;

import org.apache.commons.lang.StringUtils;
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;

@InterceptorRefs({
    @InterceptorRef("myDefault"),
    @InterceptorRef("login")
})

@Results({
    @Result(name="success", type="redirectAction", 
                params = {"actionName" , "helpdoc"}),
    @Result(name="redirect", location="${url}", type="redirect")
})

public class HelpdocController extends BaseController {
        private static final long serialVersionUID = 1L;
        private static Log log = LogFactory.getLog(MediaController.class);
        
        private String id;
        private String name;
        private String content;
        private String terms;
        private String helpdocIds;
        
        public String index(){
                return "index";
        }
        
        public String show(){
                return "show";
        }
        
        public String create() throws Exception{
                //long id = Long.parseLong(this.getHelpdocId());
                long id = CreationUtils.getNewHelpdocId();
                String name = this.getName();
                Helpdoc helpdoc = new Helpdoc(id, name);
                if(name==null){
                        addActionError("Name cannot be empty");
                        return "index"; 
                }
                if(this.getContent() != null){
                        helpdoc.setContent(this.getContent());
                }
                if(this.getTerms() != null){
                        StringTokenizer tokenizer = new StringTokenizer(this.getTerms(), ",");
                        while(tokenizer.hasMoreTokens()){
                                helpdoc.addTerm(tokenizer.nextToken().trim());
                        }
                }
                if(this.getHelpdocIds() != null){
                        StringTokenizer tokenizer = new StringTokenizer(this.getHelpdocIds(), ",");
                        while(tokenizer.hasMoreTokens()){
                                helpdoc.addrelatedDocId(Long.parseLong(tokenizer.nextToken().trim()));
                        }
                }
                log.info(helpdoc);
                CreationUtils.storeHelpdoc(helpdoc);
                return "index";
        }

        public  Map<Long, Helpdoc> getHelpdocs() throws Exception{
                return CreationUtils.getHelpdocs();
        }
        
        public  Helpdoc getHelpdoc() throws Exception{
                return CreationUtils.getHelpdoc(Long.parseLong(this.getId()));
        }
        
        public String edit(){

                return "edit";
        }
        
        public String editNew() {
        return "editNew";
    }

        
        public String destroy() throws Exception{
                long id = Long.parseLong(this.getId());
                CreationUtils.getHelpdoc(id);
                return "index";
        }
        
        public String update() throws Exception {
                long id = Long.parseLong(this.getId());
                String name = this.getName();
                Helpdoc helpdoc = CreationUtils.getHelpdoc(id);
                helpdoc.setName(name);
                helpdoc.setContent(this.getContent());
                helpdoc.setTerms(null);
                helpdoc.setRelatedDocIds(null);
                if(this.getTerms() != null){
                        StringTokenizer tokenizer = new StringTokenizer(this.getTerms(), ",");
                        while(tokenizer.hasMoreTokens()){
                                helpdoc.addTerm(tokenizer.nextToken().trim());
                        }
                }
                if(this.getHelpdocIds() != null){
                        StringTokenizer tokenizer = new StringTokenizer(this.getHelpdocIds(), ",");
                        while(tokenizer.hasMoreTokens()){
                                helpdoc.addrelatedDocId(Long.parseLong(tokenizer.nextToken().trim()));
                        }
                }
                log.info(helpdoc);
                CreationUtils.storeHelpdoc(helpdoc);
                return "index";

        }

        /**
         * @param id the id to set
         */
        public void setId(String id) {
                this.id = id;
        }

        /**
         * @return the id
         */
        public String getId() {
                return id;
        }

        /**
         * @param terms the terms to set
         */
        public void setTerms(String terms) {
                this.terms = terms;
        }

        /**
         * @return the terms
         */
        public String getTerms() {
                return terms;
        }

        /**
         * @param content the content to set
         */
        public void setContent(String content) {
                this.content = content;
        }

        /**
         * @return the content
         */
        public String getContent() {
                return content;
        }

        /**
         * @param helpdocIds the helpdocIds to set
         */
        public void setHelpdocIds(String helpdocIds) {
                this.helpdocIds = helpdocIds;
        }

        /**
         * @return the helpdocIds
         */
        public String getHelpdocIds() {
                return helpdocIds;
        }

        /**
         * @param name the name to set
         */
        public void setName(String name) {
                this.name = name;
        }

        /**
         * @return the name
         */
        public String getName() {
                return name;
        }

        public String getTerms(List<String> terms){
                return StringUtils.join(terms, ",");
        }
}