Subversion Repositories SmartDukaan

Rev

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