(no commit message)
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / referencingobjects / ReferencingObjectsView.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.bulkeditor.referencingobjects;
12
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.Comparator;
16 import java.util.List;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import org.apache.log4j.Logger;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.core.runtime.jobs.Job;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.TableViewer;
28 import org.eclipse.jface.viewers.TableViewerColumn;
29 import org.eclipse.jface.viewers.Viewer;
30 import org.eclipse.swt.SWT;
31
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Table;
35 import org.eclipse.ui.INullSelectionListener;
36
37 import org.eclipse.ui.IWorkbenchPart;
38
39 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
40 import eu.etaxonomy.cdm.api.service.IAgentService;
41 import eu.etaxonomy.cdm.api.service.ICommonService;
42 import eu.etaxonomy.cdm.api.service.IGroupService;
43
44 import eu.etaxonomy.cdm.api.service.INameService;
45 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
46 import eu.etaxonomy.cdm.api.service.IReferenceService;
47 import eu.etaxonomy.cdm.api.service.ITaxonService;
48 import eu.etaxonomy.cdm.api.service.IUserService;
49
50 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
51 import eu.etaxonomy.cdm.model.common.CdmBase;
52 import eu.etaxonomy.cdm.model.common.Group;
53
54 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
55 import eu.etaxonomy.cdm.model.common.User;
56 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
57 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
58 import eu.etaxonomy.cdm.model.reference.Reference;
59 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
60
61 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
62 import eu.etaxonomy.taxeditor.model.DescriptionHelper;
63 import eu.etaxonomy.taxeditor.model.LineSelection;
64 import eu.etaxonomy.taxeditor.store.CdmStore;
65 import eu.etaxonomy.taxeditor.view.AbstractCdmViewPart;
66
67 /**
68 * <p>ReferencingObjectsView class.</p>
69 *
70 * @author p.ciardelli
71 * @created 08.07.2009
72 * @version 1.0
73 */
74 public class ReferencingObjectsView extends AbstractCdmViewPart implements INullSelectionListener{
75 private static final Logger logger = Logger
76 .getLogger(ReferencingObjectsView.class);
77
78 /** Constant <code>ID="eu.etaxonomy.taxeditor.bulkeditor.refer"{trunked}</code> */
79 public static final String ID = "eu.etaxonomy.taxeditor.bulkeditor.view.referencingobjects";
80
81 private TableViewer viewer;
82
83 private String referencedObjectTitleCache;
84
85 /* (non-Javadoc)
86 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#createViewer(org.eclipse.swt.widgets.Composite)
87 */
88 /** {@inheritDoc} */
89 @Override
90 public void createViewer(Composite parent) {
91 selectionService.addSelectionListener(this);
92
93 viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
94 | SWT.V_SCROLL | SWT.FULL_SELECTION);
95 createColumns(viewer);
96 viewer.setContentProvider(new ReferencingObjectsContentProvider());
97 viewer.setLabelProvider(new ReferencingObjectsLabelProvider());
98 // viewer.setInput(new ArrayList<CdmBase>());
99 }
100
101 /**
102 * Create the columns for the table
103 * @param viewer
104 */
105 private void createColumns(TableViewer viewer) {
106 Table table = viewer.getTable();
107 String[] titles = {"Class", "Description", "UUID", "Object ID" };
108 int[] bounds = { 100, 200, 100, 70};
109
110 for (int i = 0; i < titles.length; i++) {
111 TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
112 column.getColumn().setText(titles[i]);
113 column.getColumn().setWidth(bounds[i]);
114 column.getColumn().setResizable(true);
115 column.getColumn().setMoveable(true);
116 }
117 table.setHeaderVisible(true);
118 table.setLinesVisible(true);
119 }
120
121 /**
122 * <p>updateReferencingObjects</p>
123 *
124 * @param entity a {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity} object.
125 */
126 public void updateReferencingObjects(final UUID entityUUID, final Class objectClass) {
127
128 final Display display = Display.getCurrent();
129
130 Job job = new Job("Update Referencing Objects") {
131
132 @Override
133 protected IStatus run(IProgressMonitor monitor) {
134 monitor.beginTask("Calculating referencing objects", 10);
135
136 monitor.worked(3);
137
138 display.asyncExec(new Runnable() {
139
140 @Override
141 public void run() {
142 if (entityUUID != null){
143 ConversationHolder conversation = CdmStore.createConversation();
144 List<CdmBase> referencingObjects = getReferencingObjects(entityUUID, objectClass);
145 conversation.close();
146 updateView(referencingObjects);
147 }
148 }
149 });
150
151 monitor.done();
152
153
154 return Status.OK_STATUS;
155 }
156 };
157
158 job.setPriority(Job.DECORATE);
159 job.schedule();
160 }
161
162 private List<CdmBase> getReferencingObjects(UUID entity, Class objectClass) {
163 CdmBase referencedObject = null;
164 try {
165 if (objectClass.getSuperclass().equals(TeamOrPersonBase.class) ){
166 referencedObject = CdmStore.getService(IAgentService.class).load(entity);
167 } else if (objectClass.getSuperclass().equals(TaxonNameBase.class)){
168 referencedObject = CdmStore.getService(INameService.class).load(entity);
169 } else if (objectClass.getSuperclass().equals(TaxonBase.class)){
170 referencedObject = CdmStore.getService(ITaxonService.class).load(entity);
171 } else if (objectClass.getSuperclass().equals(Reference.class)){
172 referencedObject = CdmStore.getService(IReferenceService.class).load(entity);
173 } else if (objectClass.getSuperclass().equals(SpecimenOrObservationBase.class)){
174 referencedObject = CdmStore.getService(IOccurrenceService.class).load(entity);
175 } else if (objectClass.isAssignableFrom(User.class)){
176 referencedObject = CdmStore.getService(IUserService.class).load(entity);
177 } else if (objectClass.isAssignableFrom(Group.class)){
178 referencedObject = CdmStore.getService(IGroupService.class).load(entity);
179 }
180 //referencedObject =(CdmBase) CdmStore.getService(IIdentifiableEntityService.class).load(referencedObject.getUuid());
181 Set<CdmBase> setOfReferencingObjects = null;
182
183 if (referencedObject != null){
184 referencedObjectTitleCache = ((IdentifiableEntity)referencedObject).getTitleCache();
185 setOfReferencingObjects = CdmStore.getService(ICommonService.class).getReferencingObjects(referencedObject);
186 }
187 if (setOfReferencingObjects != null){
188 List<CdmBase> referencingObjects = new ArrayList<CdmBase>(setOfReferencingObjects);
189
190 Collections.sort(referencingObjects, new ReferencingClassComparator());
191
192 return referencingObjects;
193
194 }
195 } catch (Exception e) {
196 logger.error("Error retrieving referencing objects", e);
197 }
198 return null;
199 }
200
201 class ReferencingClassComparator implements Comparator<CdmBase> {
202
203 /* (non-Javadoc)
204 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
205 */
206 public int compare(CdmBase o1, CdmBase o2) {
207 String string1 = o1.getClass().getSimpleName();
208 String string2 = o2.getClass().getSimpleName();
209 int classCompare = string1.compareToIgnoreCase(string2);
210 if (classCompare == 0) {
211 string1 = DescriptionHelper.getObjectDescription(o1);
212 string2 = DescriptionHelper.getObjectDescription(o2);
213 return string1.compareToIgnoreCase(string2);
214 } else {
215 return classCompare;
216 }
217 }
218 }
219
220 /**
221 *
222 */
223 private void updateView(List<CdmBase> referencingObjects) {
224 if (viewer != null && !viewer.getControl().isDisposed()){
225 viewer.setInput(referencingObjects);
226 /*if (referencedObjectTitleCache != null){
227 setContentDescription("'" + referencedObjectTitleCache + "' is referenced by:");
228 } else {
229 setContentDescription("");
230 }*/
231 showViewer();
232 }
233 }
234
235 /** {@inheritDoc} */
236 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
237 if(! (part instanceof BulkEditor)){
238 setContentDescription("");
239 showEmptyPage();
240 return;
241 }
242
243 if(! (selection instanceof LineSelection)){
244 return;
245 }
246 LineSelection lineSelection = (LineSelection) selection;
247
248 if(lineSelection.size() != 1){
249 return;
250 }
251
252 if(! (lineSelection.getFirstElement() instanceof IdentifiableEntity)){
253 return;
254 }
255
256 showViewer(part, lineSelection);
257 }
258
259 /* (non-Javadoc)
260 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#showViewer(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
261 */
262 /** {@inheritDoc} */
263 @Override
264 public void showViewer(IWorkbenchPart part, IStructuredSelection selection) {
265 this.part = part;
266
267 updateReferencingObjects(((IdentifiableEntity) selection.getFirstElement()).getUuid(),selection.getFirstElement().getClass() );
268 }
269
270 /** {@inheritDoc} */
271 @Override
272 public void dispose() {
273 selectionService.removePostSelectionListener(this);
274 selectionService.removeSelectionListener(this);
275 super.dispose();
276
277 }
278
279 /* (non-Javadoc)
280 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getViewer()
281 */
282 /** {@inheritDoc} */
283 @Override
284 public Viewer getViewer() {
285 return viewer;
286 }
287
288 /* (non-Javadoc)
289 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#dirtyStateChanged()
290 */
291 /** {@inheritDoc} */
292 @Override
293 public void changed(Object object) {
294 // no editing in this view
295 }
296
297 /* (non-Javadoc)
298 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#onComplete()
299 */
300 /** {@inheritDoc} */
301 @Override
302 public boolean onComplete() {
303 return false;
304 }
305
306 public void refresh(){
307 selectionService.removePostSelectionListener(this);
308 selectionService.removeSelectionListener(this);
309 selectionService.addPostSelectionListener(this);
310 selectionService.addSelectionListener(this);
311
312 }
313
314
315 }