Subversion Repositories SmartDukaan

Rev

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