Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
162 naveen 1
package in.shop2020.creation.controllers;
2
 
3
import java.io.PrintWriter;
4
import java.io.StringWriter;
5
import java.io.Writer;
6
import java.util.Collection;
7
import java.util.Map;
8
 
9
import in.shop2020.metamodel.core.Entity;
10
import in.shop2020.metamodel.definitions.EntityContainer;
11
import in.shop2020.metamodel.util.ExpandedEntity;
12
import in.shop2020.metamodel.util.SequenceGenerator;
13
import in.shop2020.util.DBUtils;
14
import in.shop2020.util.Utils;
15
 
16
import org.apache.juli.logging.Log;
17
import org.apache.juli.logging.LogFactory;
18
import org.apache.struts2.rest.DefaultHttpHeaders;
19
import org.apache.struts2.rest.HttpHeaders;
20
 
21
import org.apache.struts2.convention.annotation.Result;
22
import org.apache.struts2.convention.annotation.Results;
23
import org.apache.struts2.interceptor.ParameterAware;
24
 
25
@Results({
26
    @Result(name="success", type="redirectAction", 
27
    		params = {"actionName" , "entity"})
28
})
29
public class EntityController implements ParameterAware {
30
 
31
	/**
32
	 * 
33
	 */
34
	private static Log log = LogFactory.getLog(EntityController.class);
35
 
36
	/**
37
	 * 
38
	 */
39
	private String id;
40
 
41
	private ExpandedEntity expEntity;
42
 
43
	private Collection<Entity> entities;
44
 
45
	private Map<String, String[]> reqparams;
46
 
47
    // GET /entity
48
    public HttpHeaders index() {
49
    	log.info("EntityController.index");
50
 
51
    	EntityContainer ents = new EntityContainer();
52
 
53
    	try {
54
			this.entities = ents.getEntities().values();
55
		} catch (Exception e) {
56
			log.error(this.getStackTrace(e));
57
		}
58
 
59
        return new DefaultHttpHeaders("index")
60
            .disableCaching();
61
    }
62
 
63
    // GET /entity
64
    public String create() {
65
    	log.info("EntityController.create");
66
 
67
		SequenceGenerator sg;
68
		try {
69
			sg = SequenceGenerator.getInstance();
70
			long entityID = sg.getNextSequence(SequenceGenerator.ENTITY);
71
 
72
			String categoryID = this.reqparams.get("category")[0];
73
			Entity entity = new Entity(entityID, Long.parseLong(categoryID));
74
 
75
			entity.setBrand(this.reqparams.get("brand")[0]);
76
			entity.setModelName(this.reqparams.get("modelname")[0]);
77
			entity.setModelNumber(this.reqparams.get("modelnumber")[0]);
78
 
79
			EntityContainer entContainer = new EntityContainer();
80
			entContainer.addEntity(entity);
81
 
82
			DBUtils.store(entContainer.getEntities(), Utils.ENTITIES_DB_PATH + 
83
					"entities" + ".ser");
84
 
85
			String entitiesbycategoryDBFile = Utils.ENTITIES_DB_PATH + 
86
				"entitiesbycategory" + ".ser";
87
 
88
			DBUtils.store(entContainer.getEntitiesbyCategory(), 
89
					entitiesbycategoryDBFile);
90
 
91
	    	this.setId(new Long(entityID).toString());
92
		} catch (Exception e) {
93
			log.error(this.getStackTrace(e));
94
		}
95
 
96
        return edit();
97
    }
98
 
99
    // GET /entity/1/edit
100
    public String edit() {
101
    	log.info("EntityController.edit");
102
 
103
    	long entityID = Long.parseLong(this.getId());
104
    	log.info("entityID:" + entityID);
105
 
106
    	EntityContainer ents = new EntityContainer();
107
 
108
    	try {
109
			this.expEntity = ents.getExpandedEntity(entityID);
110
			log.info("this.expEntity:" + this.expEntity);
111
		} catch (Exception e) {
112
			log.error(this.getStackTrace(e));
113
		}
114
 
115
        return "edit";
116
    }
117
 
118
    // GET /entity/new
119
    public String editNew() {
120
    	log.info("EntityController.editNew");
121
 
122
        return "editNew";
123
    }
124
 
125
    // GET /entity/1
126
    public String update() {
127
    	log.info("EntityController.update");
128
 
129
        return "?";
130
    }
131
 
132
    /**
133
	 * @return the entities
134
	 */
135
	public Collection<Entity> getEntities() {
136
		return entities;
137
	}
138
 
139
    /**
140
	 * @return the entities
141
	 */
142
	public ExpandedEntity getEntity() {
143
		return expEntity;
144
	}
145
 
146
 
147
	/**
148
     * 
149
     * @param id
150
     */
151
    public void setId(String id) {
152
        this.id = id;
153
    }
154
 
155
    /**
156
     * 
157
     */
158
    public String getId() {
159
    	return this.id;
160
    }
161
 
162
	@Override
163
	public void setParameters(Map<String, String[]> reqmap) {
164
		log.info("setParameters:" + reqmap);
165
 
166
		this.reqparams = reqmap;
167
	}
168
 
169
	/**
170
	 * 
171
	 * @param aThrowable
172
	 * @return
173
	 */
174
	private String getStackTrace(Throwable aThrowable) {
175
	    final Writer result = new StringWriter();
176
	    final PrintWriter printWriter = new PrintWriter(result);
177
	    aThrowable.printStackTrace(printWriter);
178
	    return result.toString();
179
	}
180
 
181
}