d1645a7019a67456c9de0a82249b36b83a06971c
[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.action.MenuManager;
26 import org.eclipse.jface.viewers.CellEditor;
27 import org.eclipse.jface.viewers.ICellModifier;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.ITableLabelProvider;
31 import org.eclipse.jface.viewers.TableViewer;
32 import org.eclipse.jface.viewers.TableViewerColumn;
33 import org.eclipse.jface.viewers.TextCellEditor;
34 import org.eclipse.jface.viewers.TreeNode;
35 import org.eclipse.jface.viewers.Viewer;
36 import org.eclipse.swt.SWT;
37 import org.eclipse.swt.widgets.Composite;
38 import org.eclipse.swt.widgets.Control;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.swt.widgets.Menu;
41 import org.eclipse.swt.widgets.Table;
42 import org.eclipse.ui.IWorkbenchPart;
43
44 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
45 import eu.etaxonomy.cdm.api.service.IAgentService;
46 import eu.etaxonomy.cdm.api.service.IDescriptionService;
47 import eu.etaxonomy.cdm.api.service.IGroupService;
48 import eu.etaxonomy.cdm.api.service.INameService;
49 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
50 import eu.etaxonomy.cdm.api.service.IReferenceService;
51 import eu.etaxonomy.cdm.api.service.ITaxonService;
52 import eu.etaxonomy.cdm.api.service.IUserService;
53 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
54 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
55 import eu.etaxonomy.cdm.model.common.CdmBase;
56 import eu.etaxonomy.cdm.model.common.Group;
57 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
58 import eu.etaxonomy.cdm.model.common.User;
59 import eu.etaxonomy.cdm.model.description.DescriptionBase;
60 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
61 import eu.etaxonomy.cdm.model.name.NonViralName;
62 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
63 import eu.etaxonomy.cdm.model.reference.Reference;
64 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
65 import eu.etaxonomy.taxeditor.model.DescriptionHelper;
66 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
67 import eu.etaxonomy.taxeditor.store.CdmStore;
68 import eu.etaxonomy.taxeditor.view.AbstractCdmViewPart;
69
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 public static final String ID = "eu.etaxonomy.taxeditor.bulkeditor.view.referencingobjects";
80
81 private TableViewer viewer;
82
83 private String referencedObjectTitleCache;
84 private ConversationHolder conversation;
85 private ICdmEntitySession cdmEntitySession;
86
87 public ReferencingObjectsView() {
88 cdmEntitySession = CdmStore.getCurrentSessionManager().bindNullSession();
89 }
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.setCellModifier(new ICellModifier() {
102 @Override
103 public void modify(Object element, String property, Object value) {
104 }
105
106 @Override
107 public Object getValue(Object element, String property) {
108 ITableLabelProvider tableLabelProvider = null;
109 if(viewer.getLabelProvider() instanceof ITableLabelProvider){
110 tableLabelProvider = (ITableLabelProvider) viewer.getLabelProvider();
111 }
112 Object[] columnProperties = viewer.getColumnProperties();
113 for (int i=0;i<columnProperties.length;i++) {
114 if(columnProperties[i].equals(property) && tableLabelProvider!=null){
115 return tableLabelProvider.getColumnText(element, i);
116 }
117 }
118 return "";
119 }
120
121 @Override
122 public boolean canModify(Object element, String property) {
123 return true;
124 }
125 });
126
127 //create context menu
128 MenuManager menuManager = new MenuManager();
129 getSite().registerContextMenu(menuManager, viewer);
130 Control control = viewer.getControl();
131 Menu menu = menuManager.createContextMenu(control);
132 control.setMenu(menu);
133
134 // Propagate selection from viewer
135 getSite().setSelectionProvider(viewer);
136 }
137
138 /**
139 * Create the columns for the table
140 * @param viewer
141 */
142 private void createColumns(TableViewer viewer) {
143 Table table = viewer.getTable();
144 String[] titles = {"Class", "Description", "UUID", "Object ID" };
145 int[] bounds = { 100, 200, 100, 70};
146
147 CellEditor[] editors = new CellEditor[titles.length];
148
149 for (int i = 0; i < titles.length; i++) {
150 TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
151 editors[i] = new TextCellEditor(table);
152 column.getColumn().setText(titles[i]);
153 column.getColumn().setWidth(bounds[i]);
154 column.getColumn().setResizable(true);
155 column.getColumn().setMoveable(true);
156 }
157 table.setHeaderVisible(true);
158 table.setLinesVisible(true);
159
160 viewer.setCellEditors(editors);
161 viewer.setColumnProperties(titles);
162 }
163
164 @Override
165 public void setFocus() {
166 cdmEntitySession.bind();
167 }
168
169 public void updateReferencingObjects(final UUID entityUUID, final Class objectClass) {
170
171 final Display display = Display.getCurrent();
172
173 Job job = new Job("Update Referencing Objects") {
174
175 @Override
176 protected IStatus run(IProgressMonitor monitor) {
177 monitor.beginTask("Calculating referencing objects", 10);
178
179 monitor.worked(3);
180
181 display.asyncExec(new Runnable() {
182
183 @Override
184 public void run() {
185 if (entityUUID != null){
186 ICdmEntitySession previousCdmEntitySession = CdmStore.getCurrentSessionManager().getActiveSession();
187 cdmEntitySession.bind();
188 List<CdmBase> referencingObjects = getReferencingObjects(entityUUID, objectClass);
189 if(previousCdmEntitySession != null) {
190 previousCdmEntitySession.bind();
191 }
192 updateView(referencingObjects);
193
194 }
195 }
196 });
197
198 monitor.done();
199
200
201 return Status.OK_STATUS;
202 }
203 };
204
205 job.setPriority(Job.DECORATE);
206 job.schedule();
207 }
208
209 private List<CdmBase> getReferencingObjects(UUID entity, Class objectClass) {
210 CdmBase referencedObject = null;
211 try {
212 if (objectClass.getSuperclass().equals(TeamOrPersonBase.class) ){
213 referencedObject = CdmStore.getService(IAgentService.class).load(entity);
214 } else if (objectClass.getSuperclass().equals(NonViralName.class)){
215 referencedObject = CdmStore.getService(INameService.class).load(entity);
216 } else if (objectClass.getSuperclass().equals(TaxonBase.class)){
217 referencedObject = CdmStore.getService(ITaxonService.class).load(entity);
218 } else if (objectClass.equals(Reference.class)){
219 referencedObject = CdmStore.getService(IReferenceService.class).load(entity);
220 } else if (objectClass.getSuperclass().equals(SpecimenOrObservationBase.class)){
221 referencedObject = CdmStore.getService(IOccurrenceService.class).load(entity);
222 } else if (objectClass.isAssignableFrom(User.class)){
223 referencedObject = CdmStore.getService(IUserService.class).load(entity);
224 } else if (objectClass.isAssignableFrom(Group.class)){
225 referencedObject = CdmStore.getService(IGroupService.class).load(entity);
226 } else if (DescriptionBase.class.isAssignableFrom(objectClass)){
227 referencedObject = CdmStore.getService(IDescriptionService.class).load(entity);
228 } else if (DescriptionElementBase.class.isAssignableFrom(objectClass)){
229 referencedObject = CdmStore.getService(IDescriptionService.class).loadDescriptionElement(entity, null);
230 }
231 //referencedObject =(CdmBase) CdmStore.getService(IIdentifiableEntityService.class).load(referencedObject.getUuid());
232 Set<CdmBase> setOfReferencingObjects = null;
233
234 if (referencedObject != null){
235 if(referencedObject.isInstanceOf(IdentifiableEntity.class)){
236 referencedObjectTitleCache = (HibernateProxyHelper.deproxy(referencedObject, IdentifiableEntity.class)).getTitleCache();
237 }
238 else if(referencedObject.isInstanceOf(DescriptionElementBase.class)){
239 referencedObjectTitleCache = DescriptionHelper.getLabel(referencedObject);
240 }
241 else{
242 referencedObjectTitleCache = null;
243 }
244 setOfReferencingObjects = CdmStore.getCommonService().getReferencingObjects(referencedObject);
245 }
246 if (setOfReferencingObjects != null){
247 List<CdmBase> referencingObjects = new ArrayList<CdmBase>(setOfReferencingObjects);
248
249 Collections.sort(referencingObjects, new ReferencingClassComparator());
250
251 return referencingObjects;
252
253 }
254 } catch (Exception e) {
255 logger.error("Error retrieving referencing objects", e);
256 setContentDescription("The referencing objects view could not be loaded completely. Some Problems occured.");
257 }
258 return null;
259 }
260
261 class ReferencingClassComparator implements Comparator<CdmBase> {
262
263 @Override
264 public int compare(CdmBase o1, CdmBase o2) {
265 String string1 = o1.getClass().getSimpleName();
266 String string2 = o2.getClass().getSimpleName();
267 int classCompare = string1.compareToIgnoreCase(string2);
268 if (classCompare == 0) {
269 string1 = DescriptionHelper.getObjectDescription(o1);
270 string2 = DescriptionHelper.getObjectDescription(o2);
271 return string1.compareToIgnoreCase(string2);
272 } else {
273 return classCompare;
274 }
275 }
276 }
277
278 private void updateView(List<CdmBase> referencingObjects) {
279 if (viewer != null && !viewer.getControl().isDisposed()){
280 try{
281 viewer.setInput(referencingObjects);
282 if (referencedObjectTitleCache != null){
283 setContentDescription("'" + referencedObjectTitleCache + "' is referenced by:");
284 } else {
285 setContentDescription("");
286 }
287 showViewer();
288 }catch(Exception e){
289 setContentDescription("The referencing objects view could not be loaded completely. Some Problems occured.");
290 }
291
292 }
293 }
294
295 /** {@inheritDoc} */
296 @Override
297 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
298 if(part == this){
299 return;
300 }
301 if(selection instanceof IStructuredSelection){
302 showViewer(part, (IStructuredSelection) selection);
303 }
304 }
305
306 /** {@inheritDoc} */
307 @Override
308 public void showViewer(IWorkbenchPart part, IStructuredSelection selection) {
309 // this.part = part;
310
311 Object firstElement = selection.getFirstElement();
312 if(firstElement instanceof TreeNode){
313 firstElement = ((TreeNode) firstElement).getValue();
314 }
315 if(firstElement instanceof CdmBase){
316 updateReferencingObjects(((CdmBase) firstElement).getUuid(),firstElement.getClass() );
317 }
318 else{
319 updateView(null);
320 setContentDescription("");
321 }
322 }
323
324 /** {@inheritDoc} */
325 @Override
326 public void dispose() {
327 conversation.close();
328 selectionService.removePostSelectionListener(this);
329 selectionService.removeSelectionListener(this);
330 cdmEntitySession.dispose();
331 super.dispose();
332
333 }
334
335 /** {@inheritDoc} */
336 @Override
337 public Viewer getViewer() {
338 return viewer;
339 }
340
341 /** {@inheritDoc} */
342 @Override
343 public void changed(Object object) {
344 // no editing in this view
345 }
346
347 /** {@inheritDoc} */
348 @Override
349 public boolean onComplete() {
350 return false;
351 }
352
353 public void refresh(){
354 selectionService.removePostSelectionListener(this);
355 selectionService.removeSelectionListener(this);
356 selectionService.addPostSelectionListener(this);
357 selectionService.addSelectionListener(this);
358
359 }
360
361 }