Subversion Repositories SmartDukaan

Rev

Rev 5945 | Rev 9253 | 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();
5109 amit.gupta 162
						if(ffc==null){
163
							continue;
164
						}
5059 amit.gupta 165
						Map<String, Media> mediaMap = ffc.getMedias();
166
						if(mediaMap==null){
167
							continue;
168
						}
169
						for(Media media : mediaMap.values()){
170
							if(media.getType().equals(Type.IMAGE) || media.getType().equals(Type.DOCUMENT)){
171
								copyMedia(String.valueOf(entityId), dupEntityId, media);
172
							}
173
						}
174
					}
5012 amit.gupta 175
					List<Long> slideSequence = dupEntity.getSlideSequence();
176
					List<Long> newSlideSequence = new ArrayList<Long>(slideSequence);
177
					newEntity.setSlides(newSlides);
178
					newEntity.setSlideSequence(newSlideSequence);
179
					CreationUtils.updateEntity(newEntity);
180
				} catch (Exception e) {
7471 amit.gupta 181
					addActionError(dupEntityId + " is not a valid Entity.");
5012 amit.gupta 182
					e.printStackTrace();
183
					return "success";
184
				}
185
				addActionMessage("Entity " + entityId + " has been cloned successfully");
186
				return "success";
187
			}
188
 
189
 
1051 rajveer 190
			if(action.equalsIgnoreCase("ready")){
191
				if(!UserManager.getUserManager().canMarkReady(getUsername(), entityId)){
192
					addActionError("You do not have rights to mark entity as ready");
193
					return "success";	
194
				}
195
				try {
2078 rajveer 196
				    EntityState state = CreationUtils.getEntityState(entityId);
197
 
3127 rajveer 198
					CatalogClient csc = new CatalogClient();
5945 mandeep.dh 199
					in.shop2020.model.v1.catalog.CatalogService.Client iclient = csc.getClient();
2078 rajveer 200
					iclient.markItemAsContentComplete(entityId, state.getCategoryID(), state.getBrand(), state.getModelName(), state.getModelNumber());	
1051 rajveer 201
 
2095 rajveer 202
                    List<Long> inconsistentEntities = CreationUtils.getInconsistentEntities();
203
                    if(inconsistentEntities != null){
204
                        inconsistentEntities.remove(state.getID());
205
                        CreationUtils.storeInconsistentEntities(inconsistentEntities);
206
                    }
2078 rajveer 207
 
1051 rajveer 208
					state.readyEntity(getUsername());
209
					CreationUtils.updateEntityState(state);
1338 rajveer 210
					addActionMessage("Entity marked as ready successfully");
1051 rajveer 211
				} catch (Exception e) {
212
					addActionError("Unable to mark entity as ready.");
213
					e.printStackTrace();
214
				}
215
				return "success";
216
			}
4011 rajveer 217
 
218
 
219
			if(action.equalsIgnoreCase("iteminfo")){
220
				return "iteminfo";
221
			}
222
 
223
 
725 chandransh 224
		}
1051 rajveer 225
		addActionError("There is some problem, please contact admin.");
725 chandransh 226
		return "success";
227
	}
228
 
1051 rajveer 229
 
725 chandransh 230
	public String getId(){
231
		return this.id;
232
	}
233
 
234
	public void setId(String id){
235
		this.id = id;
236
	}
237
 
5059 amit.gupta 238
	/**
239
	 * 
240
	 * @throws Exception
241
	 */
242
	private void copyMedia(String newEntityId, String oldEntityId, Media media) throws Exception {
243
		String mediaDirPath = Utils.CONTENT_DB_PATH + "media" + File.separator + newEntityId;
244
		File mediaDir = new File(mediaDirPath);
245
		if(!mediaDir.exists()) {
246
			mediaDir.mkdir();
247
		} 
248
 
249
		String mediaFilePath = mediaDirPath + File.separator + media.getFileName();
250
		String oldMediaFilePath = media.getLocation();
251
 
252
		File mediaFile = new File(mediaFilePath);
253
		log.info("Media File ---------" + mediaFile);
254
		mediaFile.createNewFile();
255
 
256
		File oldMediaFile = new File(oldMediaFilePath);
257
 
258
		InputStream in = new FileInputStream(oldMediaFile);
259
 
260
		// appending output stream
261
		// @rajveer : replacing the existing file 
262
		OutputStream out = new FileOutputStream(mediaFile); 
263
 
264
		try {
265
			IOUtils.copy(in, out);
266
		}
267
		finally {
268
			IOUtils.closeQuietly(in);
269
			IOUtils.closeQuietly(out);
270
		}
271
		media.setLocation(mediaFilePath);
272
	}
273
 
725 chandransh 274
}