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