all delete methods need an Uuid as argument
[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 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(NonViralName.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.getCommonService().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 try{
230 viewer.setInput(referencingObjects);
231 if (referencedObjectTitleCache != null){
232 setContentDescription("'" + referencedObjectTitleCache + "' is referenced by:");
233 } else {
234 setContentDescription("");
235 }
236 showViewer();
237 }catch(Exception e){
238 setContentDescription("Some Problems occured.");
239 }
240
241 }
242 }
243
244 /** {@inheritDoc} */
245 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
246 if(! (part instanceof BulkEditor)){
247 setContentDescription("");
248 showEmptyPage();
249 return;
250 }
251
252 if(! (selection instanceof LineSelection)){
253 return;
254 }
255 LineSelection lineSelection = (LineSelection) selection;
256
257 if(lineSelection.size() != 1){
258 return;
259 }
260
261 if(! (lineSelection.getFirstElement() instanceof IdentifiableEntity)){
262 return;
263 }
264
265 showViewer(part, lineSelection);
266 }
267
268 /* (non-Javadoc)
269 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#showViewer(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
270 */
271 /** {@inheritDoc} */
272 @Override
273 public void showViewer(IWorkbenchPart part, IStructuredSelection selection) {
274 // this.part = part;
275
276 updateReferencingObjects(((IdentifiableEntity) selection.getFirstElement()).getUuid(),selection.getFirstElement().getClass() );
277 }
278
279 /** {@inheritDoc} */
280 @Override
281 public void dispose() {
282 conversation.close();
283 selectionService.removePostSelectionListener(this);
284 selectionService.removeSelectionListener(this);
285 super.dispose();
286
287 }
288
289 /* (non-Javadoc)
290 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getViewer()
291 */
292 /** {@inheritDoc} */
293 @Override
294 public Viewer getViewer() {
295 return viewer;
296 }
297
298 /* (non-Javadoc)
299 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#dirtyStateChanged()
300 */
301 /** {@inheritDoc} */
302 @Override
303 public void changed(Object object) {
304 // no editing in this view
305 }
306
307 /* (non-Javadoc)
308 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#onComplete()
309 */
310 /** {@inheritDoc} */
311 @Override
312 public boolean onComplete() {
313 return false;
314 }
315
316 public void refresh(){
317
318 selectionService.removePostSelectionListener(this);
319 selectionService.removeSelectionListener(this);
320 selectionService.addPostSelectionListener(this);
321 selectionService.addSelectionListener(this);
322
323 }
324
325
326 }