editor now updatable via updateSite
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / handler / SaveTaxonHandler.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.editor.handler;
11
12 import java.util.Set;
13
14 import org.apache.log4j.Logger;
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.ui.handlers.HandlerUtil;
20
21 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
22 import eu.etaxonomy.cdm.model.taxon.Taxon;
23 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
24 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
25 import eu.etaxonomy.taxeditor.editor.EditorUtil;
26 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
27 import eu.etaxonomy.taxeditor.editor.name.IterableSynonymyList;
28
29 /**
30 * @author n.hoffmann
31 * @created 23.03.2009
32 * @version 1.0
33 */
34 public class SaveTaxonHandler extends AbstractHandler {
35 private static final Logger logger = Logger
36 .getLogger(SaveTaxonHandler.class);
37
38 /* (non-Javadoc)
39 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
40 */
41 public Object execute(ExecutionEvent event) throws ExecutionException {
42
43 MultiPageTaxonEditor editor = EditorUtil.getActiveEditor();
44 // check for non zero length of taxon name
45 if(isNameZeroLength(editor.getTaxon().getName())){
46 MessageDialog.openWarning(HandlerUtil.getActiveShell(event), "No Name Specified", "An attempt was made to save a taxon with " +
47 "an empty name. Operation was cancelled.");
48 return null;
49 }
50
51 if (!areRelatedNamesNonZeroLength(editor.getTaxon())) {
52 MessageDialog.openWarning(HandlerUtil.getActiveShell(event), "Zero-Length Name", "One of this taxon's relationships " +
53 "has a zero-length name. Operation was cancelled.");
54 logger.trace("Tero length name detected.");
55 return null;
56 }
57
58 return EditorUtil.getActivePage().saveEditor(editor, false);
59
60 }
61
62 /**
63 * @param name
64 * @return
65 */
66 private boolean isNameZeroLength(TaxonNameBase<?, ?> name) {
67 if (name == null) {
68 return true;
69 }
70 return (name.getFullTitleCache().length() == 0);
71 }
72
73 /**
74 * @param taxon
75 * @return
76 */
77 private boolean areRelatedNamesNonZeroLength(Taxon taxon) {
78 for (TaxonBase<?> taxonBase : new IterableSynonymyList(taxon)) {
79 if (isNameZeroLength(taxonBase.getName()) ) {
80 return false;
81 }
82 }
83 Set<TaxonRelationship> taxonRelations = taxon.getTaxonRelations();
84 for (TaxonRelationship relationship : taxonRelations) {
85 if (isNameZeroLength(relationship.getToTaxon().getName()) ||
86 isNameZeroLength(relationship.getFromTaxon().getName())) {
87 return false;
88 }
89 }
90 return true;
91 }
92 }