View Javadoc

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