View Javadoc

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