Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.creation.controllers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

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.core.EntityStatus;
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);
                                                if(state.getStatus() == EntityStatus.READY){
                                                    Calendar cl = Calendar.getInstance();
                                    if(cl.get(Calendar.HOUR_OF_DAY) > 14 && cl.get(Calendar.HOUR_OF_DAY) < 16){
                                        addActionError("Sorry.... This is not right time to assign this entity. Please visit after 4 PM.");
                                        return "success";   
                                    }
                                                    List<Long> inconsistentEntities = CreationUtils.getInconsistentEntities();
                                                    if(inconsistentEntities == null){
                                                        inconsistentEntities = new ArrayList<Long>();
                                                    }
                                                    inconsistentEntities.add(state.getID());
                                                    CreationUtils.storeInconsistentEntities(inconsistentEntities);
                                                }
                                                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 {
                                    EntityState state = CreationUtils.getEntityState(entityId);
                                    
                                        CatalogServiceClient csc = new CatalogServiceClient();
                                        in.shop2020.model.v1.catalog.InventoryService.Client iclient = csc.getClient();
                                        iclient.markItemAsContentComplete(entityId, state.getCategoryID(), state.getBrand(), state.getModelName(), state.getModelNumber());     
                                        
                    List<Long> inconsistentEntities = CreationUtils.getInconsistentEntities();
                    if(inconsistentEntities != null){
                        inconsistentEntities.remove(state.getID());
                        CreationUtils.storeInconsistentEntities(inconsistentEntities);
                    }
                                        
                                        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;
        }
        
}