Subversion Repositories SmartDukaan

Rev

Rev 7585 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.creation.controllers;

import in.shop2020.content.security.UserManager;
import in.shop2020.metamodel.core.EntityState;
import in.shop2020.metamodel.core.ExpertReview;
import in.shop2020.metamodel.core.ExpertReviewSource;
import in.shop2020.metamodel.core.ExpertReviewStatus;
import in.shop2020.metamodel.util.CreationUtils;
import in.shop2020.metamodel.util.ExpandedEntity;
import in.shop2020.util.EntityUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;

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" , "expert-review"}),
    @Result(name="redirect", location="${url}", type="redirect")
})

public class ExpertReviewController extends BaseController {
        
        /**
         * 
         */
        private String entityId;
        private String entityName;
        private String sourceUrl;
        private String id;
        private String index;
        private String previous;

        private String createdTimeStamp;
        private String source;
        private String expertReviewHtml;
        private String url;
        
        private static final long serialVersionUID = 1L;
        
        public String index(){
                return "index";
        }
        
        public String editNew(){
                if(!UserManager.getUserManager().canAddRemoveReview(getUsername())){
                        addActionError("You dont have permission to Create review");
                        setUrl("/expert-review");
                        return "redirect";
                } else {
                return "edit-new";
                }
        }
        
        public String show(){
                return "show";
        }
        
        public String destroy(){
                if(canDelete()) {
                        try {
                                CreationUtils.deleteExpertReviews(Long.parseLong(entityId));
                        } catch (NumberFormatException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }else {
                        addActionError("You do not have permission to delete");
                }
                this.url = "/expert-review";
                return "redirect";
        }
        
        public String delete() throws Exception{
                if(canAddDelete()) {
                        try {
                                CreationUtils.deleteExpertReview(Long.parseLong(entityId), Integer.parseInt(index));
                        } catch (NumberFormatException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }else {
                        addActionError("You do not have permission to delete this review");
                }
                this.url = "/expert-review/" + entityId;
                return "redirect";
        }
        
        public String publish() throws Exception{
                Long eId = Long.parseLong(entityId);
                if(canPublish()) {
                        try {
                                List<ExpertReview> expReviewList = CreationUtils.getExpertReviewByEntity(eId);
                                int currentIndex = Integer.parseInt(index);
                                ExpertReview er = expReviewList.remove(currentIndex);
                                er.setStatus(ExpertReviewStatus.PUBLISHED);
                                expReviewList.add(currentIndex, er);
                                CreationUtils.storeExpertReview(eId, expReviewList);
                                touchEntityState(eId);

                        } catch (NumberFormatException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }else {
                        addActionError("You cant publish this review");
                }
                this.url = "/expert-review/" + entityId;
                return "redirect";
        }
        
        public String sendForApproval() throws Exception{
                Long eId = Long.parseLong(entityId);
                if(canSendForApproval()) {
                        try {
                                List<ExpertReview> expReviewList = CreationUtils.getExpertReviewByEntity(eId);
                                int currentIndex = Integer.parseInt(index);
                                ExpertReview er = expReviewList.remove(currentIndex);
                                er.setStatus(ExpertReviewStatus.PENDING_APPROVAL);
                                expReviewList.add(currentIndex, er);
                                CreationUtils.storeExpertReview(eId, expReviewList);
                                
                        } catch (NumberFormatException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }else {
                        addActionError("You cant publish this review");
                }
                this.url = "/expert-review/" + entityId;
                return "redirect";
        }
        
        public String approve() throws Exception{
                Long eId = Long.parseLong(entityId);
                if(canApprove()) {
                        try {
                                List<ExpertReview> expReviewList = CreationUtils.getExpertReviewByEntity(eId);
                                int currentIndex = Integer.parseInt(index);
                                ExpertReview er = expReviewList.remove(currentIndex);
                                er.setStatus(ExpertReviewStatus.APPROVED);
                                er.setApprovedBy(getUsername());
                                expReviewList.add(currentIndex, er);
                                CreationUtils.storeExpertReview(eId, expReviewList);
                        } catch (NumberFormatException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }else {
                        addActionError("You cant approve this review");
                }
                this.url = "/expert-review/" + entityId;
                return "redirect";
        }
        
        private void touchEntityState(Long eId) throws Exception {
                EntityState es = CreationUtils.getEntityState(eId);
                es.setMerkedReadyOn(new Date());
                CreationUtils.updateEntityState(es);
        }

        public List<EntityState> getReviews() throws Exception{
                List<EntityState> reviewList = new ArrayList<EntityState>();
                Set<Long> entitySet = CreationUtils.getExpertReviews().keySet();
                for (Long entityId : entitySet){
                        EntityState es = CreationUtils.getEntityState(entityId);
                        reviewList.add(es);
                }
                return reviewList;
        }
        
        public String create(){
                if(canCreate()) {
                        setUrl("/expert-review/" + entityId);
                        Long entityID = Long.parseLong(entityId);
                        try {
                                if(CreationUtils.getEntity(entityID) != null){
                                        Date d  = new Date();
                                        ExpertReview er = new ExpertReview(d);
                                        er.setReviewContent(expertReviewHtml);
                                        er.setUrl(sourceUrl);
                                        er.setCreatedBy(getUsername());
                                        er.setSource(source);
                                        er.setStatus(ExpertReviewStatus.CREATED);
                                        if(canApprove(er)) {
                                                er.setStatus(ExpertReviewStatus.APPROVED);
                                        }else {
                                        }
                                        CreationUtils.storeExpertReview(entityID, Arrays.asList(er));
                                        return "redirect";
                                }
                        } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }else {
                        this.addActionError("You dont have rights to create entity");
                }
                return "show";
                
        }
        public String getEntityId() {
                return entityId;
        }
        
        public void setEntityId(String entityId) {
                this.entityId = entityId;
        }
        
        public String getCreatedTimeStamp() {
                return createdTimeStamp;
        }
        
        public List<ExpertReview> getExpertReviewByEntity(){
                try {
                        return CreationUtils.getExpertReviewByEntity(Long.parseLong(entityId));
                } catch (Exception e) {
                        e.printStackTrace();
                        return null;
                }
        }
        
        public void setCreatedTimeStamp(String createdTimeStamp) {
                this.createdTimeStamp = createdTimeStamp;
        }
        
        public String getSource() {
                return source;
        }
        
        public void setSource(String source) {
                this.source = source;
        }
        
        public String getExpertReviewHtml() {
                return expertReviewHtml;
        }
        
                public void setExpertReviewHtml(String expertReviewHtml) {
                        this.expertReviewHtml = expertReviewHtml;
                }

        public String getUrl() {
                return url;
        }

        public void setUrl(String url) {
                this.url = url;
        }

        public void setId(String id) {
                this.id = id;
                this.entityId = id;
                ExpandedEntity expEntity;
                try {
                        expEntity = CreationUtils.getExpandedEntity(Long.parseLong(entityId));
                        this.entityName = EntityUtils.getProductName(expEntity);
                } catch (NumberFormatException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

        public String getId() {
                return id;
        }

        public void setEntityName(String entityName) {
                this.entityName = entityName;
        }

        public String getEntityName() {
                return entityName;
        }
        
        public String move(){
                int index = Integer.parseInt(this.index);
                int previous = Integer.parseInt(this.previous);
                Long eId = Long.parseLong(entityId);
                try {
                        List<ExpertReview> expReviewList = CreationUtils.getExpertReviewByEntity(eId);
                        expReviewList.add(index, expReviewList.remove(previous));
                        CreationUtils.storeExpertReview(eId, expReviewList);
                        touchEntityState(eId);
                } catch (NumberFormatException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                url="/expert-review/" + id;
                return "redirect";
        }
        
        public String change(){
                int index = Integer.parseInt(this.index);
                Long eId = Long.parseLong(entityId);
                try {
                        List<ExpertReview> expReviewList = CreationUtils.getExpertReviewByEntity(eId);
                        ExpertReview er = expReviewList.remove(index);
                        er.setReviewContent(expertReviewHtml);
                        expReviewList.add(index, er);
                        CreationUtils.storeExpertReview(eId, expReviewList);
                        touchEntityState(eId);
                } catch (NumberFormatException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                url="/expert-review/" + id;
                return "redirect";
        }

        public void setPrevious(String previous) {
                this.previous = previous;
        }

        public String getPrevious() {
                return previous;
        }

        public void setIndex(String index) {
                this.index = index;
        }

        public String getIndex() {
                return index;
        }
        
        public boolean canDelete(){
                return UserManager.getUserManager().canRemoveReview(getUsername());
        }
        
        public boolean canAddDelete() throws Exception{
                return canAddDelete(Integer.parseInt(index));
        }
        
        public boolean canAddDelete(ExpertReview expertReview) throws Exception{
                if(expertReview.getCreatedBy().equals(getUsername()) && (expertReview.getStatus().equals(ExpertReviewStatus.CREATED) || 
                                expertReview.getStatus().equals(ExpertReviewStatus.PENDING_APPROVAL))) {
                        return UserManager.getUserManager().canAddRemoveReview(getUsername());
                } else { 
                        return UserManager.getUserManager().canRemoveReview(getUsername()); 
                }
        }
        
        public boolean canAddDelete(int index) throws Exception{
                Long entityid = Long.parseLong(getEntityId());
                List<ExpertReview> er = CreationUtils.getExpertReviewByEntity(entityid);
                ExpertReview expertReview = er.get(index);
                return canAddDelete(expertReview);
        }
        
        public boolean canOrder(){
                return UserManager.getUserManager().canOrderReview(getUsername());
        }
        
        public boolean canApprove(int index) throws Exception{
                Long entityid = Long.parseLong(getEntityId());
                List<ExpertReview> er = CreationUtils.getExpertReviewByEntity(entityid);
                ExpertReview expertReview = er.get(index);
                return canApprove(expertReview);
        }

        public boolean canApprove() throws Exception{
                return canApprove(Integer.parseInt(index));
        }

        public boolean canApprove(ExpertReview expertReview) throws Exception{
                if(expertReview.getStatus().equals(ExpertReviewStatus.PENDING_APPROVAL) || (expertReview.getStatus().equals(ExpertReviewStatus.CREATED) && expertReview.getCreatedBy().equals(getUsername()))) {
                        return UserManager.getUserManager().canApproveReview(getUsername());
                } else {
                        return false;
                }
        }
        
        public boolean canPublish(int index) throws Exception{
                Long entityid = Long.parseLong(getEntityId());
                List<ExpertReview> er = CreationUtils.getExpertReviewByEntity(entityid);
                ExpertReview expertReview = er.get(index);
                return canPublish(expertReview);
        }
        
        public boolean canPublish() throws Exception{
                return canPublish(Integer.parseInt(index));
        }
        
        public boolean canPublish(ExpertReview expertReview) throws Exception{
                if(expertReview.getStatus().equals(ExpertReviewStatus.APPROVED)) {
                        return UserManager.getUserManager().canPublishReview(getUsername());
                } else {
                        return false;
                }
        }
        
        
        public boolean canSendForApproval(int index) throws Exception{
                Long entityid = Long.parseLong(getEntityId());
                List<ExpertReview> er = CreationUtils.getExpertReviewByEntity(entityid);
                ExpertReview expertReview = er.get(index);
                return canSendForApproval(expertReview);
        }
        
        public boolean canSendForApproval() throws Exception{
                return canSendForApproval(Integer.parseInt(index));
        }
        
        public boolean canSendForApproval(ExpertReview expertReview){
                if (expertReview.getCreatedBy().equals(getUsername())){
                        return expertReview.getStatus().equals(ExpertReviewStatus.CREATED);
                }else {
                        return false;
                }
        }
        
        public boolean canPhaseOut(ExpertReview expertReview) throws Exception{
                if(expertReview.getStatus().equals(ExpertReviewStatus.PHASED_OUT)) {
                        return UserManager.getUserManager().canPhaseOutReview(getUsername());
                } else {
                        return false;
                }
        }
        
        public boolean canCreate(){
                return UserManager.getUserManager().canCreateReview(getUsername());
        }
        
        public Set<ExpertReviewSource> getSources() throws Exception {
                return CreationUtils.getExpertReviewSources();
        }

        public void setSourceUrl(String sourceUrl) {
                this.sourceUrl = sourceUrl;
        }

        public String getSourceUrl() {
                return sourceUrl;
        }
        
}