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