Subversion Repositories SmartDukaan

Rev

Rev 9253 | Rev 19685 | 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;
9253 rajveer 5
import in.shop2020.metamodel.core.Bullet;
1051 rajveer 6
import in.shop2020.metamodel.core.Entity;
7
import in.shop2020.metamodel.core.EntityState;
2095 rajveer 8
import in.shop2020.metamodel.core.EntityStatus;
9253 rajveer 9
import in.shop2020.metamodel.core.EnumDataObject;
10
import in.shop2020.metamodel.core.Feature;
5059 amit.gupta 11
import in.shop2020.metamodel.core.FreeformContent;
12
import in.shop2020.metamodel.core.Media;
13
import in.shop2020.metamodel.core.Media.Type;
5012 amit.gupta 14
import in.shop2020.metamodel.core.Slide;
9253 rajveer 15
import in.shop2020.metamodel.definitions.Catalog;
1051 rajveer 16
import in.shop2020.metamodel.util.CreationUtils;
3127 rajveer 17
import in.shop2020.thrift.clients.CatalogClient;
5059 amit.gupta 18
import in.shop2020.util.Utils;
725 chandransh 19
 
5012 amit.gupta 20
import java.io.ByteArrayInputStream;
21
import java.io.ByteArrayOutputStream;
5059 amit.gupta 22
import java.io.File;
23
import java.io.FileInputStream;
24
import java.io.FileOutputStream;
25
import java.io.InputStream;
5012 amit.gupta 26
import java.io.ObjectInputStream;
27
import java.io.ObjectOutputStream;
5059 amit.gupta 28
import java.io.OutputStream;
5012 amit.gupta 29
import java.util.ArrayList;
30
import java.util.Calendar;
31
import java.util.List;
5059 amit.gupta 32
import java.util.Map;
5012 amit.gupta 33
 
5059 amit.gupta 34
import org.apache.commons.io.IOUtils;
5012 amit.gupta 35
import org.apache.juli.logging.Log;
36
import org.apache.juli.logging.LogFactory;
1051 rajveer 37
import org.apache.struts2.convention.annotation.InterceptorRef;
38
import org.apache.struts2.convention.annotation.InterceptorRefs;
725 chandransh 39
import org.apache.struts2.convention.annotation.Result;
40
import org.apache.struts2.convention.annotation.Results;
41
 
1051 rajveer 42
@InterceptorRefs({
43
    @InterceptorRef("myDefault"),
44
    @InterceptorRef("login")
45
})
725 chandransh 46
 
47
@Results({
48
    @Result(name="success", type="redirectAction", 
49
    		params = {"actionName" , "entity"})
50
})
1051 rajveer 51
public class ItemUpdateController extends BaseController {
725 chandransh 52
 
53
	private static final long serialVersionUID = 7180013499003793197L;
54
 
55
	private String id;
56
 
5012 amit.gupta 57
	private static Log log = LogFactory.getLog(ItemUpdateController.class);
58
 
725 chandransh 59
	public String show(){
1051 rajveer 60
		if(this.reqparams.get("action") !=null){
725 chandransh 61
			long entityId = Long.parseLong(getId());
1051 rajveer 62
			String action = this.reqparams.get("action")[0];
63
 
64
			if(action.equalsIgnoreCase("complete")){
65
				if(!UserManager.getUserManager().canComplete(getUsername(), entityId)){
66
					addActionError("Can not complete because this entity is not assigned to you.");
67
					return "success";	
68
				}
69
				try {
70
					Entity entity = CreationUtils.getEntity(entityId);
71
					ContentValidator validator = new ContentValidator();
72
					if(!validator.validate(entity)){
73
						addActionError("Entity is not yet complete because some mandatory fields are not filled.");
74
						return "success";
75
					}
76
					EntityState state = CreationUtils.getEntityState(entityId);
77
					state.completeEntity(this.getUsername());
78
					CreationUtils.updateEntityState(state);
79
				} catch (Exception e) {
80
					e.printStackTrace();
81
				}
82
				addActionMessage("Entity completed successfully");
83
				return "success";
84
			}
85
 
86
			if(action.equalsIgnoreCase("assign")){
87
				if(this.reqparams.get("username") !=null){
2095 rajveer 88
				    if(!UserManager.getUserManager().canAssign(getUsername(), entityId)){
1051 rajveer 89
						addActionError("You do not have rights to assign an entity");
90
						return "success";	
91
					}
92
					String assignTo = this.reqparams.get("username")[0];
93
					System.out.println("User name is " + assignTo);
94
					try {
95
						EntityState state = CreationUtils.getEntityState(entityId);
2095 rajveer 96
						if(state.getStatus() == EntityStatus.READY){
97
						    Calendar cl = Calendar.getInstance();
4011 rajveer 98
						    int hourOfDay = cl.get(Calendar.HOUR_OF_DAY);
4012 rajveer 99
						    int minuteOfHour = cl.get(Calendar.MINUTE);
4011 rajveer 100
						    int totalMinutes = 60*hourOfDay + minuteOfHour;  
101
		                    if(totalMinutes > 14*60 + 45 && totalMinutes < 15*60 + 30){
4012 rajveer 102
		                        addActionError("Sorry.... This is not right time to assign this entity. Please visit after 3.30 PM.");
2095 rajveer 103
		                        return "success";   
104
		                    }
105
						    List<Long> inconsistentEntities = CreationUtils.getInconsistentEntities();
106
						    if(inconsistentEntities == null){
107
						        inconsistentEntities = new ArrayList<Long>();
108
						    }
109
						    inconsistentEntities.add(state.getID());
110
						    CreationUtils.storeInconsistentEntities(inconsistentEntities);
111
						}
1051 rajveer 112
						state.assignEntity(this.getUsername(), assignTo);
113
						CreationUtils.updateEntityState(state);
114
					} catch (Exception e) {
115
						e.printStackTrace();
116
					}
117
					addActionMessage("Entity assigned to " + assignTo + " successfully");
118
					return "success";
119
				}
120
			}
121
 
5012 amit.gupta 122
			if (action.equalsIgnoreCase("dup-entity")) {
123
				if (!UserManager.getUserManager().canAssign(getUsername(),
124
						entityId)) {
125
					addActionError("You do not have rights to clone entites.");
126
					return "success";
127
				}
128
				String dupEntityId = this.reqparams.get("entity-old")[0];
129
				log.info("Duplicate Entity is " + dupEntityId);
130
				try {
131
					Entity newEntity = CreationUtils.getEntity(entityId);
132
					Entity dupEntity = CreationUtils.getEntity(Long
133
							.parseLong(dupEntityId));
5059 amit.gupta 134
 
5012 amit.gupta 135
					List<Slide> slides = dupEntity.getSlides();
136
					List<Slide> newSlides = new ArrayList<Slide>();
137
					for (Slide slide : slides) {
138
						// Serializing and deserializing to clone the slide
139
						// serialize
140
						ByteArrayOutputStream bos = new ByteArrayOutputStream();
141
						ObjectOutputStream out = new ObjectOutputStream(bos);
142
						out.writeObject(slide);
1051 rajveer 143
 
5012 amit.gupta 144
						// De-serialization
145
						ByteArrayInputStream bis = new ByteArrayInputStream(
146
								bos.toByteArray());
147
						ObjectInputStream in = new ObjectInputStream(bis);
148
						Slide newSlide = (Slide) in.readObject();
149
						newSlides.add(newSlide);
150
					}
5059 amit.gupta 151
					//for all new slides update the mediatypes and create new mediastore for this entity
152
					List<Slide> allSlides = new ArrayList<Slide>();
153
					allSlides.addAll(newSlides);
154
					for(Slide sl: newSlides){
155
						if(sl.hasChildrenSlides()){
156
							allSlides.addAll(sl.getChildrenSlides());
157
							for(Slide sl1 : sl.getChildrenSlides()){
158
								if(sl1.hasChildrenSlides()){
159
									allSlides.addAll(sl.getChildrenSlides());
160
								}
161
							}
162
						}
163
					}
164
					for(Slide sl : allSlides){
165
						FreeformContent ffc = sl.getFreeformContent();
5109 amit.gupta 166
						if(ffc==null){
167
							continue;
168
						}
5059 amit.gupta 169
						Map<String, Media> mediaMap = ffc.getMedias();
170
						if(mediaMap==null){
171
							continue;
172
						}
173
						for(Media media : mediaMap.values()){
174
							if(media.getType().equals(Type.IMAGE) || media.getType().equals(Type.DOCUMENT)){
175
								copyMedia(String.valueOf(entityId), dupEntityId, media);
176
							}
177
						}
178
					}
5012 amit.gupta 179
					List<Long> slideSequence = dupEntity.getSlideSequence();
180
					List<Long> newSlideSequence = new ArrayList<Long>(slideSequence);
181
					newEntity.setSlides(newSlides);
182
					newEntity.setSlideSequence(newSlideSequence);
183
					CreationUtils.updateEntity(newEntity);
184
				} catch (Exception e) {
7471 amit.gupta 185
					addActionError(dupEntityId + " is not a valid Entity.");
5012 amit.gupta 186
					e.printStackTrace();
187
					return "success";
188
				}
189
				addActionMessage("Entity " + entityId + " has been cloned successfully");
190
				return "success";
191
			}
192
 
193
 
1051 rajveer 194
			if(action.equalsIgnoreCase("ready")){
195
				if(!UserManager.getUserManager().canMarkReady(getUsername(), entityId)){
196
					addActionError("You do not have rights to mark entity as ready");
197
					return "success";	
198
				}
199
				try {
2078 rajveer 200
				    EntityState state = CreationUtils.getEntityState(entityId);
201
 
9253 rajveer 202
				    //For all android phones in mobile category, we should enable data pack. 
203
				    boolean isAndroid = false;
204
				    if(state.getCategoryID() == 10006){
205
				    	Entity e = CreationUtils.getEntity(entityId);
206
				    	Slide slide = e.getSlide(130020);
207
				    	if(slide != null)  {
208
				    		List<Feature> fs = slide.getFeatures();
209
				    		if (fs!=null){
210
				    			for (Feature f1 : fs){
211
				    				if(f1.getFeatureDefinitionID()==120043l){
212
				    					List <Bullet> bullets = f1.getBullets();
213
				    					if(bullets != null) {
214
				    						for(Bullet b : bullets){
215
				    							String bulletString = Catalog.getInstance().getDefinitionsContainer().getEnumValue(((EnumDataObject)b.getDataObject()).getEnumValueID()).getValue();
216
				    							if (bulletString.contains("Android")){
217
				    								isAndroid = true;
12555 amit.gupta 218
				    								log.info("Is android marked true for " + entityId);
9253 rajveer 219
				    								break;
220
				    							}
221
				    						}
222
				    					}
223
				    				}
224
				    			}
225
				    		}
226
				    	}
227
				    }
228
 
3127 rajveer 229
					CatalogClient csc = new CatalogClient();
5945 mandeep.dh 230
					in.shop2020.model.v1.catalog.CatalogService.Client iclient = csc.getClient();
9253 rajveer 231
					iclient.markItemAsContentComplete(entityId, state.getCategoryID(), state.getBrand(), state.getModelName(), state.getModelNumber(), isAndroid);	
1051 rajveer 232
 
2095 rajveer 233
                    List<Long> inconsistentEntities = CreationUtils.getInconsistentEntities();
234
                    if(inconsistentEntities != null){
235
                        inconsistentEntities.remove(state.getID());
236
                        CreationUtils.storeInconsistentEntities(inconsistentEntities);
237
                    }
2078 rajveer 238
 
1051 rajveer 239
					state.readyEntity(getUsername());
240
					CreationUtils.updateEntityState(state);
1338 rajveer 241
					addActionMessage("Entity marked as ready successfully");
1051 rajveer 242
				} catch (Exception e) {
243
					addActionError("Unable to mark entity as ready.");
244
					e.printStackTrace();
245
				}
246
				return "success";
247
			}
4011 rajveer 248
 
249
 
250
			if(action.equalsIgnoreCase("iteminfo")){
251
				return "iteminfo";
252
			}
253
 
254
 
725 chandransh 255
		}
1051 rajveer 256
		addActionError("There is some problem, please contact admin.");
725 chandransh 257
		return "success";
258
	}
259
 
1051 rajveer 260
 
725 chandransh 261
	public String getId(){
262
		return this.id;
263
	}
264
 
265
	public void setId(String id){
266
		this.id = id;
267
	}
268
 
5059 amit.gupta 269
	/**
270
	 * 
271
	 * @throws Exception
272
	 */
273
	private void copyMedia(String newEntityId, String oldEntityId, Media media) throws Exception {
274
		String mediaDirPath = Utils.CONTENT_DB_PATH + "media" + File.separator + newEntityId;
275
		File mediaDir = new File(mediaDirPath);
276
		if(!mediaDir.exists()) {
277
			mediaDir.mkdir();
278
		} 
279
 
280
		String mediaFilePath = mediaDirPath + File.separator + media.getFileName();
281
		String oldMediaFilePath = media.getLocation();
282
 
283
		File mediaFile = new File(mediaFilePath);
284
		log.info("Media File ---------" + mediaFile);
285
		mediaFile.createNewFile();
286
 
287
		File oldMediaFile = new File(oldMediaFilePath);
288
 
289
		InputStream in = new FileInputStream(oldMediaFile);
290
 
291
		// appending output stream
292
		// @rajveer : replacing the existing file 
293
		OutputStream out = new FileOutputStream(mediaFile); 
294
 
295
		try {
296
			IOUtils.copy(in, out);
297
		}
298
		finally {
299
			IOUtils.closeQuietly(in);
300
			IOUtils.closeQuietly(out);
301
		}
302
		media.setLocation(mediaFilePath);
303
	}
304
 
725 chandransh 305
}