View Javadoc

1   /**
2    * 
3    */
4   package de.tivsource.page.admin.actions.others.slider;
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.slider.SliderDaoLocal;
19  import de.tivsource.page.entity.slider.Slider;
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 = -850874829898683018L;
32  
33      /**
34       * Statischer Logger der Klasse.
35       */
36      private static final Logger LOGGER = LogManager.getLogger(JsonAction.class);
37  	
38  	@InjectEJB(name="SliderDao")
39      private SliderDaoLocal newsDaoLocal;
40  
41  	private List<Slider> gridModel;
42  	private List<Slider> sliderList;
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.*.slider"}) }
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.newsDaoLocal.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  				sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.uuid", "asc");
89  			} else if (getSidx() != null && getSidx().equalsIgnoreCase("clickable")) {
90  				sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.clickable", "asc");
91  			} else if (getSidx() != null && getSidx().equalsIgnoreCase("url")) {
92                  sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.url", "asc");
93              } else if (getSidx() != null && getSidx().equalsIgnoreCase("name")) {
94                  sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.name", "asc");
95              } else if (getSidx() != null && getSidx().equalsIgnoreCase("page")) {
96                  sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.page", "asc");
97              } else if (getSidx() != null && getSidx().equalsIgnoreCase("orderNumber")) {
98                  sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.orderNumber", "asc");
99              } else if (getSidx() != null && getSidx().equalsIgnoreCase("created")) {
100 				sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.created", "asc");
101 			} else if (getSidx() != null && getSidx().equalsIgnoreCase("visible")) {
102 				sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.visible", "asc");
103 			} else if (getSidx() != null && getSidx().equalsIgnoreCase("modified")) {
104                 sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.modified", "asc");
105             } else if (getSidx() != null && getSidx().equalsIgnoreCase("modifiedBy")) {
106                 sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.modifiedBy", "asc");
107             } else if (getSidx() != null && getSidx().equalsIgnoreCase("modifiedAddress")) {
108                 sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.modifiedAddress", "asc");
109             } else {
110 				sliderList = this.newsDaoLocal.findAll(from, getRows());
111 			}
112 		} else if (getSord() != null && getSord().equalsIgnoreCase("desc")) {
113 		    LOGGER.info("Sortieren nach desc");
114 			if (getSidx() != null && getSidx().equalsIgnoreCase("uuid")) {
115 				sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.uuid", "desc");
116 			} else if (getSidx() != null && getSidx().equalsIgnoreCase("clickable")) {
117                 sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.clickable", "desc");
118             } else if (getSidx() != null && getSidx().equalsIgnoreCase("url")) {
119 				sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.url", "desc");
120 			} else if (getSidx() != null && getSidx().equalsIgnoreCase("name")) {
121                 sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.name", "desc");
122             } else if (getSidx() != null && getSidx().equalsIgnoreCase("page")) {
123                 sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.page", "desc");
124             } else if (getSidx() != null && getSidx().equalsIgnoreCase("orderNumber")) {
125 				sliderList = this.newsDaoLocal.findAll(from, getRows(), "s.orderNumber", "desc");
126 			} else if (getSidx() != null && getSidx().equalsIgnoreCase("created")) {
127 				sliderList = this.newsDaoLocal.findAll(from, getRows(), "n.created", "desc");
128 			} else if (getSidx() != null && getSidx().equalsIgnoreCase("visible")) {
129 				sliderList = this.newsDaoLocal.findAll(from, getRows(), "n.visible", "desc");
130 			} else if (getSidx() != null && getSidx().equalsIgnoreCase("modified")) {
131                 sliderList = this.newsDaoLocal.findAll(from, getRows(), "n.modified", "desc");
132             } else if (getSidx() != null && getSidx().equalsIgnoreCase("modifiedBy")) {
133                 sliderList = this.newsDaoLocal.findAll(from, getRows(), "n.modifiedBy", "desc");
134             } else if (getSidx() != null && getSidx().equalsIgnoreCase("modifiedAddress")) {
135                 sliderList = this.newsDaoLocal.findAll(from, getRows(), "n.modifiedAddress", "desc");
136             } else {
137 				sliderList = this.newsDaoLocal.findAll(from, getRows());
138 			}
139 		}
140 
141 		setTotal((int) Math.ceil((double) getRecord() / (double) getRows()));
142 		setGridModel(sliderList);
143 
144 		LOGGER.info("Rows:" + rows);
145 		LOGGER.info("Page:" + page);
146 		LOGGER.info("Total:" + total);
147 		LOGGER.info("Record:" + record);
148 		LOGGER.info("Sord:" + sord);
149 		LOGGER.info("Sidx:" + sidx);
150 		
151 		return execute();
152 	}
153 
154 	/**
155 	 * @return how many rows we want to have into the grid
156 	 */
157 	public Integer getRows() {
158 		return rows;
159 	}
160 
161 	/**
162 	 * @param rows
163 	 *            how many rows we want to have into the grid
164 	 */
165 	public void setRows(Integer rows) {
166 		this.rows = rows;
167 	}
168 
169 	/**
170 	 * @return current page of the query
171 	 */
172 	public Integer getPage() {
173 		return page;
174 	}
175 
176 	/**
177 	 * @param page
178 	 *            current page of the query
179 	 */
180 	public void setPage(Integer page) {
181 		this.page = page;
182 	}
183 
184 	/**
185 	 * @return total pages for the query
186 	 */
187 	public Integer getTotal() {
188 		return total;
189 	}
190 
191 	/**
192 	 * @param total
193 	 *            total pages for the query
194 	 */
195 	public void setTotal(Integer total) {
196 		this.total = total;
197 	}
198 
199 	/**
200 	 * @return total number of records for the query. e.g. select count(*) from
201 	 *         table
202 	 */
203 	public Integer getRecord() {
204 		return record;
205 	}
206 
207 	/**
208 	 * @param record
209 	 *            total number of records for the query. e.g. select count(*)
210 	 *            from table
211 	 */
212 	public void setRecord(Integer record) {
213 
214 		this.record = record;
215 
216 		if (this.record > 0 && this.rows > 0) {
217 			this.total = (int) Math.ceil((double) this.record
218 					/ (double) this.rows);
219 		} else {
220 			this.total = 0;
221 		}
222 	}
223 
224 	/**
225 	 * @return an collection that contains the actual data
226 	 */
227 	public List<Slider> getGridModel() {
228 		return gridModel;
229 	}
230 
231 	/**
232 	 * @param gridModel
233 	 *            an collection that contains the actual data
234 	 */
235 	public void setGridModel(List<Slider> gridModel) {
236 		this.gridModel = gridModel;
237 	}
238 
239 	/**
240 	 * @return sorting order
241 	 */
242 	public String getSord() {
243 		return sord;
244 	}
245 
246 	/**
247 	 * @param sord
248 	 *            sorting order
249 	 */
250 	public void setSord(String sord) {
251 		this.sord = sord;
252 	}
253 
254 	/**
255 	 * @return get index row - i.e. user click to sort.
256 	 */
257 	public String getSidx() {
258 		return sidx;
259 	}
260 
261 	/**
262 	 * @param sidx
263 	 *            get index row - i.e. user click to sort.
264 	 */
265 	public void setSidx(String sidx) {
266 		this.sidx = sidx;
267 	}
268 
269 }// Ende class