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