Mercurial > hg > ismi-richfaces
annotate src/main/java/de/mpiwg/itgroup/ismi/browse/EntityRepositoryBean.java @ 169:0b5d02012299 public_by_author
more work on publicByAuthor feature.
author | Robert Casties <casties@mpiwg-berlin.mpg.de> |
---|---|
date | Thu, 31 May 2018 20:26:10 +0200 |
parents | 29bd63f749c6 |
children | 631864bfec2e |
rev | line source |
---|---|
1 | 1 package de.mpiwg.itgroup.ismi.browse; |
2 | |
103 | 3 import java.io.IOException; |
4 import java.io.PrintWriter; | |
1 | 5 import java.util.ArrayList; |
103 | 6 import java.util.List; |
1 | 7 |
103 | 8 import javax.faces.context.FacesContext; |
1 | 9 import javax.faces.event.ActionEvent; |
103 | 10 import javax.servlet.http.HttpServletResponse; |
1 | 11 |
12 import org.apache.commons.lang.StringUtils; | |
13 import org.mpi.openmind.repository.bo.Entity; | |
103 | 14 import org.mpi.openmind.repository.utils.CsvEntityListWriter; |
1 | 15 |
16 import de.mpiwg.itgroup.ismi.entry.beans.SessionBean; | |
17 import de.mpiwg.itgroup.ismi.event.beans.CopyEvent; | |
18 import de.mpiwg.itgroup.ismi.event.beans.StudyEvent; | |
19 import de.mpiwg.itgroup.ismi.event.beans.TransferEvent; | |
20 import de.mpiwg.itgroup.ismi.util.guiComponents.DataPaginator; | |
21 | |
22 public class EntityRepositoryBean extends AbstractEntityRepositoryBean { | |
103 | 23 |
1 | 24 private static final long serialVersionUID = -2380877853539157567L; |
25 | |
169
0b5d02012299
more work on publicByAuthor feature.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
146
diff
changeset
|
26 protected transient DataPaginator paginator = new DataPaginator(); |
1 | 27 |
28 public EntityRepositoryBean(){ | |
29 super(); | |
30 } | |
31 | |
32 @Override | |
33 public void reset(){ | |
34 super.reset(); | |
35 this.paginator = new DataPaginator(); | |
36 } | |
37 | |
38 | |
169
0b5d02012299
more work on publicByAuthor feature.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
146
diff
changeset
|
39 protected void updateEntities() { |
103 | 40 if (StringUtils.isNotEmpty(getObjectClass())) { |
41 this.paginator.initCount(); | |
42 int startRecord = this.paginator.getCurrentPage() * this.paginator.getItemsPerPage(); | |
43 | |
44 if (this.paginator.getNumberOfPages() == 0) { | |
45 this.setEntities(new ArrayList<Entity>()); | |
46 } else { | |
47 int mod = getWrapper().getEntitiesCount(getObjectClass()) % paginator.getItemsPerPage(); | |
48 if ((paginator.getCurrentPage() + 1) == paginator.getNumberOfPages() && mod != 0) { | |
49 this.setEntities( | |
50 getWrapper().getEntityByDefSubList(getObjectClass(), startRecord, startRecord + mod)); | |
51 } else { | |
52 this.setEntities(getWrapper().getEntityByDefSubList(getObjectClass(), startRecord, | |
53 startRecord + paginator.getItemsPerPage())); | |
54 } | |
55 } | |
56 } else { | |
57 this.setEntities(new ArrayList<Entity>()); | |
58 } | |
59 } | |
1 | 60 |
61 public String first() { | |
62 this.paginator.first(); | |
63 this.updateEntities(); | |
64 return GOTO_ENTITY_REPOSITORY; | |
65 } | |
66 | |
67 public String last() { | |
68 this.paginator.last(); | |
69 this.updateEntities(); | |
70 return GOTO_ENTITY_REPOSITORY; | |
71 } | |
72 | |
73 public String fastForward() { | |
74 this.paginator.fastForward(); | |
75 this.updateEntities(); | |
76 return GOTO_ENTITY_REPOSITORY; | |
77 } | |
78 | |
79 public String fastRewind() { | |
80 this.paginator.fastRewind(); | |
81 this.updateEntities(); | |
82 return GOTO_ENTITY_REPOSITORY; | |
83 } | |
84 | |
85 public String previous() { | |
86 this.paginator.previous(); | |
87 this.updateEntities(); | |
88 return GOTO_ENTITY_REPOSITORY; | |
89 } | |
90 | |
91 public String next() { | |
92 this.paginator.next(); | |
93 this.updateEntities(); | |
94 return GOTO_ENTITY_REPOSITORY; | |
95 } | |
96 | |
97 public void listenerGoToPage(ActionEvent event) { | |
98 try { | |
99 this.setPageMsg(""); | |
100 Integer intPage = new Integer(this.getPage()); | |
101 if (intPage != null) { | |
102 intPage--; | |
103 this.paginator.goToPage(intPage); | |
104 this.updateEntities(); | |
105 | |
106 } | |
107 } catch (Exception e) { | |
108 this.setPageMsg("page is invalid!"); | |
109 } | |
110 } | |
111 | |
112 public String actionShowAll() { | |
113 | |
114 this.setResultMode(MODE_ALL); | |
115 this.setResultSummaryMsg(""); | |
116 this.setPage(""); | |
117 this.paginator.setCurrentPage(0); | |
118 int entitiesCount = getWrapper().getEntitiesCount(getObjectClass()); | |
119 this.paginator.resetNumberOfPages(entitiesCount); | |
120 this.updateEntities(); | |
121 | |
122 setResultSummaryMsg(entitiesCount + " items were found!"); | |
123 | |
124 return GOTO_ENTITY_REPOSITORY; | |
125 } | |
126 | |
127 | |
128 public String details() { | |
129 Entity selectedEntity = (Entity) getRequestBean("entity"); | |
130 EntityDetailsBean bean = (EntityDetailsBean) getRequestBean(SESSION_BEAN_ENTITY_DETAILS); | |
131 bean.setEntity(selectedEntity); | |
132 | |
133 return GOTO_ENTITY_DETAILS; | |
134 } | |
135 | |
136 public String actionEdit() { | |
137 Entity entity = (Entity) getRequestBean("entity"); | |
138 getSessionBean().editEntity(entity); | |
139 //return SessionBean.PAGE_ENTRY; | |
140 | |
141 if(entity.getObjectClass().equals(StudyEvent.OC) || | |
142 entity.getObjectClass().equals(CopyEvent.OC) || | |
143 entity.getObjectClass().equals(TransferEvent.OC)){ | |
144 return SessionBean.PAGE_EVENT_FORM; | |
145 }else{ | |
146 return SessionBean.PAGE_ENTRY; | |
147 } | |
148 } | |
103 | 149 |
150 /** | |
151 * Downloads all Entities of the selected class as CSV. | |
152 * | |
153 * To be used for resultMode == all. | |
154 * | |
155 * @throws IOException | |
156 */ | |
157 public void downloadAllCsv() throws IOException { | |
158 List<Entity> allEnts = getWrapper().getEntitiesByDef(getObjectClass()); | |
159 sendAsCsv(allEnts); | |
160 } | |
161 | |
162 /** | |
163 * Downloads all currently selected Entities as CSV. | |
164 * | |
165 * To be used for resultMode == advanced. | |
166 * | |
167 * @throws IOException | |
168 */ | |
169 public void downloadAdvancedCsv() throws IOException { | |
170 sendAsCsv(getEntities()); | |
171 } | |
172 | |
173 /** | |
174 * Send the given Entities as CSV file to the client. | |
175 * | |
176 * @throws IOException | |
177 */ | |
178 public void sendAsCsv(List<Entity> entList) throws IOException { | |
179 // Get the FacesContext | |
180 FacesContext facesContext = FacesContext.getCurrentInstance(); | |
181 | |
182 // Get HTTP response | |
183 HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse(); | |
184 | |
185 // Set response headers | |
186 response.reset(); // Reset the response in the first place | |
187 response.setCharacterEncoding("UTF-8"); | |
188 response.setHeader("Content-Type", "application/csv"); // Set the content type | |
189 response.setHeader("Content-Disposition", "attachment; filename=entities.csv"); | |
190 | |
191 // Open response output Writer | |
192 PrintWriter responseWriter = response.getWriter(); | |
193 | |
194 // make sure all entities attributes are loaded | |
195 for (Entity entity : entList) { | |
196 if (entity.isLightweight()) { | |
197 getWrapper().getEntityContent(entity); | |
198 } | |
199 } | |
200 | |
201 // write all entities to the response | |
202 CsvEntityListWriter.writeEntities(entList, responseWriter); | |
203 | |
204 // Make sure that everything is out | |
205 responseWriter.flush(); | |
206 responseWriter.close(); | |
207 | |
208 // JSF doc: | |
209 // Signal the JavaServer Faces implementation that the HTTP response for this request has already been generated | |
210 // (such as an HTTP redirect), and that the request processing lifecycle should be terminated | |
211 // as soon as the current phase is completed. | |
212 facesContext.responseComplete(); | |
213 } | |
1 | 214 |
215 public DataPaginator getPaginator() { | |
216 return paginator; | |
217 } | |
218 | |
219 public void setPaginator(DataPaginator paginator) { | |
220 this.paginator = paginator; | |
221 } | |
222 } |