View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.core;
20  
21  
22  import java.util.*;
23  
24  import org.caleigo.toolkit.log.*;
25  
26  /*** <Description of DataSourceDescriptor>
27   *
28   * @author Dennis Zikovic
29   * @version 1.00
30   *
31   *//*
32    *
33    * WHEN        WHO               WHY & WHAT
34    * ------------------------------------------------------------------------------
35    * 2001-03-12  Dennis Zikovic    Creation
36    */
37  public abstract class AbstractDataSourceDescriptor implements IDataSourceDescriptor
38  {
39      // Constants ---------------------------------------------------------------
40      
41      // Data members ------------------------------------------------------------
42      private String mCodeName;
43      private String mSourceName;
44      private String mDisplayName;
45      private String mVersion;
46      private boolean mIsReadOnly;
47      
48      private transient Object[] mEntityList;
49      private transient List mFieldRelations;
50      
51      private transient IDataSource mDefaultDataSource;
52      
53      // Constructors ------------------------------------------------------------
54      protected AbstractDataSourceDescriptor(String codeName, String sourceName, String displayName, String version, boolean readOnly, Object[] entityList)
55      {
56          mCodeName = codeName;
57          mSourceName = sourceName;
58          mDisplayName = displayName;
59          mVersion = version;
60          mEntityList = entityList;
61          mIsReadOnly = readOnly;
62      }
63      
64      // Superclass overrides ----------------------------------------------------
65      
66      /*** Overriden for improved logging support with code name and version.
67       */
68      public String toString()
69      {
70          String str = mCodeName;
71          if(mVersion!=null)
72              str += "("+mVersion+")";
73          return str;
74      }
75      
76      // Action methods ----------------------------------------------------------
77      protected Object writeReplace() throws java.io.ObjectStreamException
78      {
79          return new Dezerializer(this.getClass().getName());
80      }
81      
82      // Access methods ----------------------------------------------------------
83      public String getCodeName()
84      {
85          return mCodeName;
86      }
87      
88      public String getSourceName()
89      {
90          return mSourceName;
91      }
92      
93      public String getDisplayName()
94      {
95          return mDisplayName;
96      }
97      
98      public String getVersion()
99      {
100         return mVersion;
101     }
102     
103     public boolean isReadOnly()
104     {
105         return mIsReadOnly;
106     }
107     
108     public int getEntityDescriptorCount()
109     {
110         return mEntityList.length;
111     }
112     
113     public IEntityDescriptor getEntityDescriptor(int index)
114     {
115         if(!(mEntityList[index] instanceof IEntityDescriptor))
116         {
117             try
118             {
119                 Class entityClass = Class.forName(mEntityList[index].toString());
120                 java.lang.reflect.Field field = entityClass.getField("instance");
121                 mEntityList[index] = (IEntityDescriptor)field.get(null);
122             }
123             catch(Exception e)
124             {
125             }
126         }
127         
128         return (IEntityDescriptor)mEntityList[index];
129     }
130     
131     /*** Note that this method could be expensive to call.
132      */
133     public IEntityDescriptor getEntityDescriptor(String codeName)
134     {
135         IEntityDescriptor entityDescriptor = null;
136         for(int j=0; entityDescriptor==null && j<this.getEntityDescriptorCount(); j++)
137             if(this.getEntityDescriptor(j).getCodeName().equals(codeName))
138                 entityDescriptor = this.getEntityDescriptor(j);
139         return entityDescriptor;
140     }
141     
142     public java.util.Iterator getEntityDescriptors()
143     {
144         return new EntityDescriptorIterator();
145     }
146     
147     public boolean contains(IEntityDescriptor entityDescriptor)
148     {
149         return entityDescriptor.getDataSourceDescriptor()==this;
150     }
151     
152     public IDataService getDefaultDataService()
153     {
154         if(mDefaultDataSource!=null)
155             return mDefaultDataSource.getDataService();
156         else
157             return null;
158     }
159     
160     public IDataSource getDefaultDataSource()
161     {
162         return mDefaultDataSource;
163     }
164     
165     public void setDefaultDataSource(IDataSource dataSource)
166     {
167         mDefaultDataSource = dataSource;
168     }
169     
170     // Help methods ------------------------------------------------------------
171     
172     /*** This help method makes possible for sub classes in other packages to
173      * create field descriptor. Note that this is the only way to make intances
174      * if the you dont wont sub class the field descriptor.
175      */
176     protected static IFieldDescriptor createFieldDescriptor(String codeName, String sourceName, String displayName, DataType dataType, int length, int flags, Object defValue)
177     {
178         // What should we do with the entity descriptor name, it canīt be null!!!
179         return new FieldDescriptor(codeName, sourceName, displayName, null, dataType, length, flags, defValue);
180     }
181     
182     // Nested classess ---------------------------------------------------------
183     protected class EntityDescriptorIterator implements Iterator, Enumeration, java.io.Serializable
184     {
185         private int mArrayIndex = 0;
186         
187         public Object next()
188         {
189             mArrayIndex++;
190             return getEntityDescriptor(mArrayIndex - 1);
191         }
192         
193         public boolean hasNext()
194         {
195             return mArrayIndex<getEntityDescriptorCount();
196         }
197         
198         public void remove()
199         {
200             throw new java.lang.UnsupportedOperationException();
201         }
202         
203         public boolean hasMoreElements()
204         {
205             return this.hasNext();
206         }
207         
208         public Object nextElement()
209         {
210             return this.next();
211         }
212     }
213     
214     protected static class Dezerializer implements java.io.Serializable
215     {
216         // Data members --------------------------------------------------------
217         protected String mClassName;
218         
219         // Constructors --------------------------------------------------------
220         public Dezerializer(String className)
221         {
222             mClassName = className;
223         }
224         
225         // Action methods ------------------------------------------------------
226         protected Object readResolve() throws java.io.ObjectStreamException
227         {
228             try
229             {
230                 Class dataSourceDescriptorClass = Class.forName(mClassName);
231                 java.lang.reflect.Field instanceField = dataSourceDescriptorClass.getField("instance");
232                 return instanceField.get(null);
233             }
234             catch (Exception e)
235             {
236                 Log.printError(this, "Error in readResolve", e);
237                 throw new java.io.InvalidObjectException(e.getMessage());
238             }
239         }
240     }
241 }