View Javadoc

1   /**
2    * 
3    */
4   package de.tivsource.page.admin.actions.locations.location;
5   
6   import java.util.List;
7   
8   import org.apache.logging.log4j.LogManager;
9   import org.apache.logging.log4j.Logger;
10  import org.apache.struts2.convention.annotation.Action;
11  import org.apache.struts2.convention.annotation.ParentPackage;
12  import org.apache.struts2.convention.annotation.Result;
13  
14  import com.opensymphony.xwork2.ActionSupport;
15  
16  import de.tivsource.ejb3plugin.InjectEJB;
17  import de.tivsource.page.dao.location.LocationDaoLocal;
18  import de.tivsource.page.entity.location.Location;
19  
20  /**
21   * @author Marc Michele
22   * 
23   */
24  @ParentPackage(value = "administratorJson")
25  public class JsonAction extends ActionSupport {
26  
27      /**
28       * Serial Version UID.
29       */
30      private static final long serialVersionUID = 1771692240110579820L;
31  
32      /**
33       * Statischer Logger der Klasse.
34       */
35      private static final Logger LOGGER = LogManager.getLogger(JsonAction.class);
36  
37      @InjectEJB(name="LocationDao")
38      private LocationDaoLocal locationDaoLocal;
39  
40  	private List<Location> gridModel;
41  	private List<Location> pageList;
42  	private Integer rows = 0;
43  	private Integer page = 1;
44  	private Integer total = 0;
45  	private Integer record = 0;
46  	private String sord;
47  	private String sidx;
48  
49  	@Override
50      @Action(
51          value = "table",
52          results = {
53              @Result(name = "success", type="json", params={"excludeProperties", "gridModel.*.events, gridModel.*.vacancies, gridModel.*.picture, gridModel.*.pictureItems"})
54          }
55      )
56  	public String execute() {
57  		return SUCCESS;
58  	}
59  
60  	public String getJSON() {
61  
62  	    LOGGER.info("Page " + getPage() + " Rows " + getRows()
63  				+ " Sorting Order " + getSord() + " Index Row :" + getSidx());
64  	    LOGGER.info("Build new List");
65  
66  		/*
67  		 * Setze die Anzahl aller Objekte in der Datenbank.
68  		 */
69  		setRecord(this.locationDaoLocal.countAll());
70  
71  		int to = (getRows() * getPage());
72  		int from = to - getRows();
73  
74  		/*
75  		 * Setze die Maximalgrenze auf die Maximale Anzahl
76  		 */
77  		if (to > getRecord()) {
78  			to = getRecord();
79  		}
80  		
81  		/*
82  		 * Sortieren aufsteigen
83  		 */
84  		if (getSord() != null && getSord().equalsIgnoreCase("asc")) {
85  		    LOGGER.info("Sortieren nach asc");
86  			if (getSidx() != null && getSidx().equalsIgnoreCase("uuid")) {
87  				pageList = this.locationDaoLocal.findAll(from, getRows(), "l.uuid", "asc");
88  			} else if (getSidx() != null && getSidx().equalsIgnoreCase("name")) {
89                  pageList = this.locationDaoLocal.findAll(from, getRows(), "dm.name", "asc");
90              } else if (getSidx() != null && getSidx().equalsIgnoreCase("zip")) {
91                  pageList = this.locationDaoLocal.findAll(from, getRows(), "l.address.zip", "asc");
92              } else if (getSidx() != null && getSidx().equalsIgnoreCase("city")) {
93                  pageList = this.locationDaoLocal.findAll(from, getRows(), "l.address.city", "asc");
94              } else if (getSidx() != null && getSidx().equalsIgnoreCase("country")) {
95                  pageList = this.locationDaoLocal.findAll(from, getRows(), "l.address.country", "asc");
96              } else if (getSidx() != null && getSidx().equalsIgnoreCase("telephone")) {
97                  pageList = this.locationDaoLocal.findAll(from, getRows(), "l.contactDetails.telephone", "asc");
98              } else if (getSidx() != null && getSidx().equalsIgnoreCase("fax")) {
99                  pageList = this.locationDaoLocal.findAll(from, getRows(), "l.contactDetails.fax", "asc");
100             } else if (getSidx() != null && getSidx().equalsIgnoreCase("mobile")) {
101                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.contactDetails.mobile", "asc");
102             } else if (getSidx() != null && getSidx().equalsIgnoreCase("event")) {
103                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.event", "asc");
104             } else if (getSidx() != null && getSidx().equalsIgnoreCase("visible")) {
105                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.visible", "asc");
106             } else if (getSidx() != null && getSidx().equalsIgnoreCase("created")) {
107 				pageList = this.locationDaoLocal.findAll(from, getRows(), "l.created", "asc");
108 			} else if (getSidx() != null && getSidx().equalsIgnoreCase("modified")) {
109                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.modified", "asc");
110             } else if (getSidx() != null && getSidx().equalsIgnoreCase("modifiedBy")) {
111                 pageList = this.locationDaoLocal.findAll(from, getRows(), "modifiedBy", "asc");
112             } else {
113 				pageList = this.locationDaoLocal.findAll(from, getRows());
114 			}
115 		} else if (getSord() != null && getSord().equalsIgnoreCase("desc")) {
116 		    LOGGER.info("Sortieren nach desc");
117             if (getSidx() != null && getSidx().equalsIgnoreCase("uuid")) {
118                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.uuid", "desc");
119             } else if (getSidx() != null && getSidx().equalsIgnoreCase("name")) {
120                 pageList = this.locationDaoLocal.findAll(from, getRows(), "dm.name", "desc");
121             } else if (getSidx() != null && getSidx().equalsIgnoreCase("zip")) {
122                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.address.zip", "desc");
123             } else if (getSidx() != null && getSidx().equalsIgnoreCase("city")) {
124                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.address.city", "desc");
125             } else if (getSidx() != null && getSidx().equalsIgnoreCase("country")) {
126                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.address.country", "desc");
127             } else if (getSidx() != null && getSidx().equalsIgnoreCase("telephone")) {
128                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.contactDetails.telephone", "desc");
129             } else if (getSidx() != null && getSidx().equalsIgnoreCase("fax")) {
130                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.contactDetails.fax", "desc");
131             } else if (getSidx() != null && getSidx().equalsIgnoreCase("mobile")) {
132                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.contactDetails.mobile", "desc");
133             } else if (getSidx() != null && getSidx().equalsIgnoreCase("event")) {
134                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.event", "desc");
135             } else if (getSidx() != null && getSidx().equalsIgnoreCase("visible")) {
136                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.visible", "desc");
137             } else if (getSidx() != null && getSidx().equalsIgnoreCase("created")) {
138                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.created", "desc");
139             } else if (getSidx() != null && getSidx().equalsIgnoreCase("modified")) {
140                 pageList = this.locationDaoLocal.findAll(from, getRows(), "l.modified", "desc");
141             } else if (getSidx() != null && getSidx().equalsIgnoreCase("modifiedBy")) {
142                 pageList = this.locationDaoLocal.findAll(from, getRows(), "modifiedBy", "desc");
143             } else {
144                 pageList = this.locationDaoLocal.findAll(from, getRows());
145             }
146 		}
147 
148 		setTotal((int) Math.ceil((double) getRecord() / (double) getRows()));
149 		setGridModel(pageList);
150 
151 		LOGGER.info("Rows:" + rows);
152 		LOGGER.info("Page:" + page);
153 		LOGGER.info("Total:" + total);
154 		LOGGER.info("Record:" + record);
155 		LOGGER.info("Sord:" + sord);
156 		LOGGER.info("Sidx:" + sidx);
157 		
158 		return execute();
159 	}
160 
161 	/**
162 	 * @return how many rows we want to have into the grid
163 	 */
164 	public Integer getRows() {
165 		return rows;
166 	}
167 
168 	/**
169 	 * @param rows
170 	 *            how many rows we want to have into the grid
171 	 */
172 	public void setRows(Integer rows) {
173 		this.rows = rows;
174 	}
175 
176 	/**
177 	 * @return current page of the query
178 	 */
179 	public Integer getPage() {
180 		return page;
181 	}
182 
183 	/**
184 	 * @param page
185 	 *            current page of the query
186 	 */
187 	public void setPage(Integer page) {
188 		this.page = page;
189 	}
190 
191 	/**
192 	 * @return total pages for the query
193 	 */
194 	public Integer getTotal() {
195 		return total;
196 	}
197 
198 	/**
199 	 * @param total
200 	 *            total pages for the query
201 	 */
202 	public void setTotal(Integer total) {
203 		this.total = total;
204 	}
205 
206 	/**
207 	 * @return total number of records for the query. e.g. select count(*) from
208 	 *         table
209 	 */
210 	public Integer getRecord() {
211 		return record;
212 	}
213 
214 	/**
215 	 * @param record
216 	 *            total number of records for the query. e.g. select count(*)
217 	 *            from table
218 	 */
219 	public void setRecord(Integer record) {
220 
221 		this.record = record;
222 
223 		if (this.record > 0 && this.rows > 0) {
224 			this.total = (int) Math.ceil((double) this.record
225 					/ (double) this.rows);
226 		} else {
227 			this.total = 0;
228 		}
229 	}
230 
231 	/**
232 	 * @return an collection that contains the actual data
233 	 */
234 	public List<Location> getGridModel() {
235 		return gridModel;
236 	}
237 
238 	/**
239 	 * @param gridModel
240 	 *            an collection that contains the actual data
241 	 */
242 	public void setGridModel(List<Location> gridModel) {
243 		this.gridModel = gridModel;
244 	}
245 
246 	/**
247 	 * @return sorting order
248 	 */
249 	public String getSord() {
250 		return sord;
251 	}
252 
253 	/**
254 	 * @param sord
255 	 *            sorting order
256 	 */
257 	public void setSord(String sord) {
258 		this.sord = sord;
259 	}
260 
261 	/**
262 	 * @return get index row - i.e. user click to sort.
263 	 */
264 	public String getSidx() {
265 		return sidx;
266 	}
267 
268 	/**
269 	 * @param sidx
270 	 *            get index row - i.e. user click to sort.
271 	 */
272 	public void setSidx(String sidx) {
273 		this.sidx = sidx;
274 	}
275 
276 }// Ende class