View Javadoc

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