Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
725 chandransh 1
package in.shop2020.creation.controllers;
2
 
2095 rajveer 3
import java.util.ArrayList;
4
import java.util.Calendar;
5
import java.util.List;
6
 
1051 rajveer 7
import in.shop2020.content.security.UserManager;
8
import in.shop2020.creation.util.ContentValidator;
9
import in.shop2020.metamodel.core.Entity;
10
import in.shop2020.metamodel.core.EntityState;
2095 rajveer 11
import in.shop2020.metamodel.core.EntityStatus;
1051 rajveer 12
import in.shop2020.metamodel.util.CreationUtils;
3127 rajveer 13
import in.shop2020.thrift.clients.CatalogClient;
725 chandransh 14
 
1051 rajveer 15
import org.apache.struts2.convention.annotation.InterceptorRef;
16
import org.apache.struts2.convention.annotation.InterceptorRefs;
725 chandransh 17
import org.apache.struts2.convention.annotation.Result;
18
import org.apache.struts2.convention.annotation.Results;
19
 
1051 rajveer 20
@InterceptorRefs({
21
    @InterceptorRef("myDefault"),
22
    @InterceptorRef("login")
23
})
725 chandransh 24
 
25
@Results({
26
    @Result(name="success", type="redirectAction", 
27
    		params = {"actionName" , "entity"})
28
})
1051 rajveer 29
public class ItemUpdateController extends BaseController {
725 chandransh 30
 
31
	private static final long serialVersionUID = 7180013499003793197L;
32
 
33
	private String id;
34
 
35
	public String show(){
1051 rajveer 36
		if(this.reqparams.get("action") !=null){
725 chandransh 37
			long entityId = Long.parseLong(getId());
1051 rajveer 38
			String action = this.reqparams.get("action")[0];
39
 
40
			if(action.equalsIgnoreCase("complete")){
41
				if(!UserManager.getUserManager().canComplete(getUsername(), entityId)){
42
					addActionError("Can not complete because this entity is not assigned to you.");
43
					return "success";	
44
				}
45
				try {
46
					Entity entity = CreationUtils.getEntity(entityId);
47
					ContentValidator validator = new ContentValidator();
48
					if(!validator.validate(entity)){
49
						addActionError("Entity is not yet complete because some mandatory fields are not filled.");
50
						return "success";
51
					}
52
					EntityState state = CreationUtils.getEntityState(entityId);
53
					state.completeEntity(this.getUsername());
54
					CreationUtils.updateEntityState(state);
55
				} catch (Exception e) {
56
					e.printStackTrace();
57
				}
58
				addActionMessage("Entity completed successfully");
59
				return "success";
60
			}
61
 
62
			if(action.equalsIgnoreCase("assign")){
63
				if(this.reqparams.get("username") !=null){
2095 rajveer 64
				    if(!UserManager.getUserManager().canAssign(getUsername(), entityId)){
1051 rajveer 65
						addActionError("You do not have rights to assign an entity");
66
						return "success";	
67
					}
68
					String assignTo = this.reqparams.get("username")[0];
69
					System.out.println("User name is " + assignTo);
70
					try {
71
						EntityState state = CreationUtils.getEntityState(entityId);
2095 rajveer 72
						if(state.getStatus() == EntityStatus.READY){
73
						    Calendar cl = Calendar.getInstance();
74
		                    if(cl.get(Calendar.HOUR_OF_DAY) > 14 && cl.get(Calendar.HOUR_OF_DAY) < 16){
75
		                        addActionError("Sorry.... This is not right time to assign this entity. Please visit after 4 PM.");
76
		                        return "success";   
77
		                    }
78
						    List<Long> inconsistentEntities = CreationUtils.getInconsistentEntities();
79
						    if(inconsistentEntities == null){
80
						        inconsistentEntities = new ArrayList<Long>();
81
						    }
82
						    inconsistentEntities.add(state.getID());
83
						    CreationUtils.storeInconsistentEntities(inconsistentEntities);
84
						}
1051 rajveer 85
						state.assignEntity(this.getUsername(), assignTo);
86
						CreationUtils.updateEntityState(state);
87
					} catch (Exception e) {
88
						e.printStackTrace();
89
					}
90
					addActionMessage("Entity assigned to " + assignTo + " successfully");
91
					return "success";
92
				}
93
			}
94
 
95
 
96
			if(action.equalsIgnoreCase("ready")){
97
				if(!UserManager.getUserManager().canMarkReady(getUsername(), entityId)){
98
					addActionError("You do not have rights to mark entity as ready");
99
					return "success";	
100
				}
101
				try {
2078 rajveer 102
				    EntityState state = CreationUtils.getEntityState(entityId);
103
 
3127 rajveer 104
					CatalogClient csc = new CatalogClient();
1051 rajveer 105
					in.shop2020.model.v1.catalog.InventoryService.Client iclient = csc.getClient();
2078 rajveer 106
					iclient.markItemAsContentComplete(entityId, state.getCategoryID(), state.getBrand(), state.getModelName(), state.getModelNumber());	
1051 rajveer 107
 
2095 rajveer 108
                    List<Long> inconsistentEntities = CreationUtils.getInconsistentEntities();
109
                    if(inconsistentEntities != null){
110
                        inconsistentEntities.remove(state.getID());
111
                        CreationUtils.storeInconsistentEntities(inconsistentEntities);
112
                    }
2078 rajveer 113
 
1051 rajveer 114
					state.readyEntity(getUsername());
115
					CreationUtils.updateEntityState(state);
1338 rajveer 116
					addActionMessage("Entity marked as ready successfully");
1051 rajveer 117
				} catch (Exception e) {
118
					addActionError("Unable to mark entity as ready.");
119
					e.printStackTrace();
120
				}
121
				return "success";
122
			}
725 chandransh 123
		}
1051 rajveer 124
		addActionError("There is some problem, please contact admin.");
725 chandransh 125
		return "success";
126
	}
127
 
1051 rajveer 128
 
725 chandransh 129
	public String getId(){
130
		return this.id;
131
	}
132
 
133
	public void setId(String id){
134
		this.id = id;
135
	}
136
 
137
}