Subversion Repositories SmartDukaan

Rev

Rev 1081 | Rev 2095 | 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.creation.util.ContentValidator;
import in.shop2020.metamodel.core.Entity;
import in.shop2020.metamodel.core.EntityState;
import in.shop2020.metamodel.util.CreationUtils;
import in.shop2020.thrift.clients.CatalogServiceClient;

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" , "entity"})
})
public class ItemUpdateController extends BaseController {
        
        private static final long serialVersionUID = 7180013499003793197L;
        
        private String id;
        
        public String show(){
                if(this.reqparams.get("action") !=null){
                        long entityId = Long.parseLong(getId());
                        String action = this.reqparams.get("action")[0];
                        
                        if(action.equalsIgnoreCase("complete")){
                                if(!UserManager.getUserManager().canComplete(getUsername(), entityId)){
                                        addActionError("Can not complete because this entity is not assigned to you.");
                                        return "success";       
                                }
                                try {
                                        Entity entity = CreationUtils.getEntity(entityId);
                                        ContentValidator validator = new ContentValidator();
                                        if(!validator.validate(entity)){
                                                addActionError("Entity is not yet complete because some mandatory fields are not filled.");
                                                return "success";
                                        }
                                        EntityState state = CreationUtils.getEntityState(entityId);
                                        state.completeEntity(this.getUsername());
                                        CreationUtils.updateEntityState(state);
                                } catch (Exception e) {
                                        e.printStackTrace();
                                }
                                addActionMessage("Entity completed successfully");
                                return "success";
                        }
                        
                        if(action.equalsIgnoreCase("assign")){
                                if(this.reqparams.get("username") !=null){
                                        if(!UserManager.getUserManager().canAssign(getUsername(), entityId)){
                                                addActionError("You do not have rights to assign an entity");
                                                return "success";       
                                        }
                                        String assignTo = this.reqparams.get("username")[0];
                                        System.out.println("User name is " + assignTo);
                                        try {
                                                EntityState state = CreationUtils.getEntityState(entityId);
                                                state.assignEntity(this.getUsername(), assignTo);
                                                CreationUtils.updateEntityState(state);
                                        } catch (Exception e) {
                                                e.printStackTrace();
                                        }
                                        addActionMessage("Entity assigned to " + assignTo + " successfully");
                                        return "success";
                                }
                        }
                        

                        if(action.equalsIgnoreCase("ready")){
                                if(!UserManager.getUserManager().canMarkReady(getUsername(), entityId)){
                                        addActionError("You do not have rights to mark entity as ready");
                                        return "success";       
                                }
                                try {
                                        CatalogServiceClient csc = new CatalogServiceClient();
                                        in.shop2020.model.v1.catalog.InventoryService.Client iclient = csc.getClient();
                                        iclient.markItemAsContentComplete(entityId);    
                                        
                                        EntityState state = CreationUtils.getEntityState(entityId);
                                        state.readyEntity(getUsername());
                                        CreationUtils.updateEntityState(state);
                                        addActionMessage("Entity marked as ready successfully");
                                } catch (Exception e) {
                                        addActionError("Unable to mark entity as ready.");
                                        e.printStackTrace();
                                }
                                return "success";
                        }
                }
                addActionError("There is some problem, please contact admin.");
                return "success";
        }

        
        public String getId(){
                return this.id;
        }
        
        public void setId(String id){
                this.id = id;
        }
        
}