View Javadoc

1   /*
2    wsmo4j - a WSMO API and Reference Implementation
3   
4    Copyright (c) 2004-2005, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  
19  package com.ontotext.wsmo4j.ontology;
20  
21  import java.lang.reflect.*;
22  
23  import org.omwg.ontology.*;
24  import org.wsmo.common.exception.*;
25  import org.wsmo.common.*;
26  
27  import com.ontotext.wsmo4j.common.EntityImpl;
28  
29  /**
30   * <p>Title: WSMO4J</p>
31   * <p>Description: WSMO API and a Reference Implementation</p>
32   * <p>Copyright:  Copyright (c) 2004-2005</p>
33   * <p>Company: OntoText Lab. / SIRMA </p>
34   * @author not attributable
35   * @version 1.0
36   *
37   */
38  
39  public abstract class OntologyElementImpl extends EntityImpl implements OntologyElement {
40  
41      private Ontology ownerOntology;
42  
43      private static final Method[] ontologyMethods = Ontology.class.getMethods();
44      
45      public OntologyElementImpl(Identifier id) {
46          super(id);
47      }
48  
49      /**
50       * Retrieve the owner of the Instance.
51       * @throws org.wsmo.common.exception.SynchronisationException
52       */
53      public Ontology getOntology() throws SynchronisationException {
54          return ownerOntology;
55      }
56  
57      /**
58       * Sets a new owner of the Instance.
59       * @throws InvalidModelException 
60       * @throws org.wsmo.common.exception.SynchronisationException
61       */
62      public void setOntology(Ontology ontology) throws InvalidModelException {
63          if ((ontology == null && ownerOntology == null) || (ontology == ownerOntology)) {
64              return;
65          }
66          if (ownerOntology != null) {
67              Ontology oldOntology = ownerOntology;
68              ownerOntology = null;
69              invokeOntologyMethod("remove", oldOntology);
70          }
71          if (ontology != null) {
72              ownerOntology = ontology;
73              invokeOntologyMethod("add", ontology);
74          }
75      }
76  
77      private void invokeOntologyMethod(String operation, Ontology ontologyToInvoke) {
78          Class[] supportedInterfaces = getClass().getInterfaces();
79  
80          // find the method from Ontology that starts with operationXXXEntity(handleToXXX)
81          for (int i = 0; i < supportedInterfaces.length; i++) {
82              for (int j = 0; j < ontologyMethods.length; j++) {
83                  if (ontologyMethods[j].getName().equals(
84                          operation + getSimpleName(supportedInterfaces[i]))
85                          && ontologyMethods[j].getParameterTypes().length == 1
86                          && getSimpleName(ontologyMethods[j].getParameterTypes()[0]).equals(
87                              getSimpleName(supportedInterfaces[i])))
88                      try {
89                          ontologyMethods[j].invoke(ontologyToInvoke, new Object[] { this });
90                          return;
91                      }
92                      catch (Exception e) {
93                          throw new RuntimeException(
94                                  "Could not " + operation + " from the ontology!", e);
95                      }
96              }
97          }
98  
99          throw new RuntimeException("Could not " + operation + " from the ontology!");
100     }
101     
102     private String getSimpleName(Class myClass) {
103         int pos = myClass.getName().lastIndexOf(".");
104         if (pos > -1 && pos + 1 < myClass.getName().length()) {
105             return myClass.getName().substring(pos+1);
106         }
107         return myClass.getName();
108     }
109     
110 }
111 
112 /*
113  * $Log$
114  * Revision 1.7  2006/03/23 16:12:13  nathaliest
115  * moving the anon Id check to the validator
116  *
117  * Revision 1.6  2006/03/01 15:01:53  vassil_momtchev
118  * Type is ambigious if compiled with java5 compiler
119  *
120  * Revision 1.5  2006/02/27 15:28:10  nathaliest
121  * fixed anonId check
122  *
123  * Revision 1.4  2006/02/27 10:22:20  nathaliest
124  * added anonId check at element.setOntology(ontology).
125  *
126  * Revision 1.3  2005/09/15 11:41:45  vassil_momtchev
127  * getShortName() removed (java 1.4 compatible)
128  *
129  * Revision 1.2  2005/09/15 08:40:00  vassil_momtchev
130  * OntologyElement.setOntology(Ontology) removes the element from the previous ontology if exists, and adds it to the new if not null
131  *
132  * Revision 1.1  2005/06/01 12:09:05  marin_dimitrov
133  * v0.4.0
134  *
135  * Revision 1.3  2005/05/12 13:46:44  marin
136  * changed to an _abstract_ class (not supposed to be used directly but only through subclassing)
137  *
138  * Revision 1.2  2005/05/11 13:35:22  alex
139  * copyright notice updated
140  *
141  * Revision 1.1  2005/05/11 12:40:58  alex
142  * initial commit
143  *
144  */