Subversion Repositories SmartDukaan

Rev

Rev 4012 | Rev 5059 | 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
 
1051 rajveer 3
import in.shop2020.content.security.UserManager;
4
import in.shop2020.creation.util.ContentValidator;
5
import in.shop2020.metamodel.core.Entity;
6
import in.shop2020.metamodel.core.EntityState;
2095 rajveer 7
import in.shop2020.metamodel.core.EntityStatus;
5012 amit.gupta 8
import in.shop2020.metamodel.core.Slide;
1051 rajveer 9
import in.shop2020.metamodel.util.CreationUtils;
3127 rajveer 10
import in.shop2020.thrift.clients.CatalogClient;
725 chandransh 11
 
5012 amit.gupta 12
import java.io.ByteArrayInputStream;
13
import java.io.ByteArrayOutputStream;
14
import java.io.ObjectInputStream;
15
import java.io.ObjectOutputStream;
16
import java.util.ArrayList;
17
import java.util.Calendar;
18
import java.util.List;
19
 
20
import org.apache.juli.logging.Log;
21
import org.apache.juli.logging.LogFactory;
1051 rajveer 22
import org.apache.struts2.convention.annotation.InterceptorRef;
23
import org.apache.struts2.convention.annotation.InterceptorRefs;
725 chandransh 24
import org.apache.struts2.convention.annotation.Result;
25
import org.apache.struts2.convention.annotation.Results;
26
 
1051 rajveer 27
@InterceptorRefs({
28
    @InterceptorRef("myDefault"),
29
    @InterceptorRef("login")
30
})
725 chandransh 31
 
32
@Results({
33
    @Result(name="success", type="redirectAction", 
34
    		params = {"actionName" , "entity"})
35
})
1051 rajveer 36
public class ItemUpdateController extends BaseController {
725 chandransh 37
 
38
	private static final long serialVersionUID = 7180013499003793197L;
39
 
40
	private String id;
41
 
5012 amit.gupta 42
	private static Log log = LogFactory.getLog(ItemUpdateController.class);
43
 
725 chandransh 44
	public String show(){
1051 rajveer 45
		if(this.reqparams.get("action") !=null){
725 chandransh 46
			long entityId = Long.parseLong(getId());
1051 rajveer 47
			String action = this.reqparams.get("action")[0];
48
 
49
			if(action.equalsIgnoreCase("complete")){
50
				if(!UserManager.getUserManager().canComplete(getUsername(), entityId)){
51
					addActionError("Can not complete because this entity is not assigned to you.");
52
					return "success";	
53
				}
54
				try {
55
					Entity entity = CreationUtils.getEntity(entityId);
56
					ContentValidator validator = new ContentValidator();
57
					if(!validator.validate(entity)){
58
						addActionError("Entity is not yet complete because some mandatory fields are not filled.");
59
						return "success";
60
					}
61
					EntityState state = CreationUtils.getEntityState(entityId);
62
					state.completeEntity(this.getUsername());
63
					CreationUtils.updateEntityState(state);
64
				} catch (Exception e) {
65
					e.printStackTrace();
66
				}
67
				addActionMessage("Entity completed successfully");
68
				return "success";
69
			}
70
 
71
			if(action.equalsIgnoreCase("assign")){
72
				if(this.reqparams.get("username") !=null){
2095 rajveer 73
				    if(!UserManager.getUserManager().canAssign(getUsername(), entityId)){
1051 rajveer 74
						addActionError("You do not have rights to assign an entity");
75
						return "success";	
76
					}
77
					String assignTo = this.reqparams.get("username")[0];
78
					System.out.println("User name is " + assignTo);
79
					try {
80
						EntityState state = CreationUtils.getEntityState(entityId);
2095 rajveer 81
						if(state.getStatus() == EntityStatus.READY){
82
						    Calendar cl = Calendar.getInstance();
4011 rajveer 83
						    int hourOfDay = cl.get(Calendar.HOUR_OF_DAY);
4012 rajveer 84
						    int minuteOfHour = cl.get(Calendar.MINUTE);
4011 rajveer 85
						    int totalMinutes = 60*hourOfDay + minuteOfHour;  
86
		                    if(totalMinutes > 14*60 + 45 && totalMinutes < 15*60 + 30){
4012 rajveer 87
		                        addActionError("Sorry.... This is not right time to assign this entity. Please visit after 3.30 PM.");
2095 rajveer 88
		                        return "success";   
89
		                    }
90
						    List<Long> inconsistentEntities = CreationUtils.getInconsistentEntities();
91
						    if(inconsistentEntities == null){
92
						        inconsistentEntities = new ArrayList<Long>();
93
						    }
94
						    inconsistentEntities.add(state.getID());
95
						    CreationUtils.storeInconsistentEntities(inconsistentEntities);
96
						}
1051 rajveer 97
						state.assignEntity(this.getUsername(), assignTo);
98
						CreationUtils.updateEntityState(state);
99
					} catch (Exception e) {
100
						e.printStackTrace();
101
					}
102
					addActionMessage("Entity assigned to " + assignTo + " successfully");
103
					return "success";
104
				}
105
			}
106
 
5012 amit.gupta 107
			if (action.equalsIgnoreCase("dup-entity")) {
108
				if (!UserManager.getUserManager().canAssign(getUsername(),
109
						entityId)) {
110
					addActionError("You do not have rights to clone entites.");
111
					return "success";
112
				}
113
				String dupEntityId = this.reqparams.get("entity-old")[0];
114
				log.info("Duplicate Entity is " + dupEntityId);
115
				try {
116
					Entity newEntity = CreationUtils.getEntity(entityId);
117
					Entity dupEntity = CreationUtils.getEntity(Long
118
							.parseLong(dupEntityId));
119
					List<Slide> slides = dupEntity.getSlides();
120
					List<Slide> newSlides = new ArrayList<Slide>();
121
					for (Slide slide : slides) {
122
						// Serializing and deserializing to clone the slide
123
						// serialize
124
						ByteArrayOutputStream bos = new ByteArrayOutputStream();
125
						ObjectOutputStream out = new ObjectOutputStream(bos);
126
						out.writeObject(slide);
1051 rajveer 127
 
5012 amit.gupta 128
						// De-serialization
129
						ByteArrayInputStream bis = new ByteArrayInputStream(
130
								bos.toByteArray());
131
						ObjectInputStream in = new ObjectInputStream(bis);
132
						Slide newSlide = (Slide) in.readObject();
133
						newSlides.add(newSlide);
134
					}
135
 
136
					List<Long> slideSequence = dupEntity.getSlideSequence();
137
					List<Long> newSlideSequence = new ArrayList<Long>(slideSequence);
138
					newEntity.setSlides(newSlides);
139
					newEntity.setSlideSequence(newSlideSequence);
140
					CreationUtils.updateEntity(newEntity);
141
				} catch (Exception e) {
142
					addActionError(dupEntityId + "is not a valid Entity.");
143
					e.printStackTrace();
144
					return "success";
145
				}
146
				addActionMessage("Entity " + entityId + " has been cloned successfully");
147
				return "success";
148
			}
149
 
150
 
1051 rajveer 151
			if(action.equalsIgnoreCase("ready")){
152
				if(!UserManager.getUserManager().canMarkReady(getUsername(), entityId)){
153
					addActionError("You do not have rights to mark entity as ready");
154
					return "success";	
155
				}
156
				try {
2078 rajveer 157
				    EntityState state = CreationUtils.getEntityState(entityId);
158
 
3127 rajveer 159
					CatalogClient csc = new CatalogClient();
1051 rajveer 160
					in.shop2020.model.v1.catalog.InventoryService.Client iclient = csc.getClient();
2078 rajveer 161
					iclient.markItemAsContentComplete(entityId, state.getCategoryID(), state.getBrand(), state.getModelName(), state.getModelNumber());	
1051 rajveer 162
 
2095 rajveer 163
                    List<Long> inconsistentEntities = CreationUtils.getInconsistentEntities();
164
                    if(inconsistentEntities != null){
165
                        inconsistentEntities.remove(state.getID());
166
                        CreationUtils.storeInconsistentEntities(inconsistentEntities);
167
                    }
2078 rajveer 168
 
1051 rajveer 169
					state.readyEntity(getUsername());
170
					CreationUtils.updateEntityState(state);
1338 rajveer 171
					addActionMessage("Entity marked as ready successfully");
1051 rajveer 172
				} catch (Exception e) {
173
					addActionError("Unable to mark entity as ready.");
174
					e.printStackTrace();
175
				}
176
				return "success";
177
			}
4011 rajveer 178
 
179
 
180
			if(action.equalsIgnoreCase("iteminfo")){
181
				return "iteminfo";
182
			}
183
 
184
 
725 chandransh 185
		}
1051 rajveer 186
		addActionError("There is some problem, please contact admin.");
725 chandransh 187
		return "success";
188
	}
189
 
1051 rajveer 190
 
725 chandransh 191
	public String getId(){
192
		return this.id;
193
	}
194
 
195
	public void setId(String id){
196
		this.id = id;
197
	}
198
 
199
}