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  /*** The IDataSource defines an interface that describes a source of data and
22   * provides the means to access that source. It can be viewed as a handle or
23   * reference to a specific individual persistent storage or database.
24   *
25   * @author  Dennis Zikovic
26   * @version 1.00
27   *
28   *//* 
29   *
30   * WHEN        WHO               WHY & WHAT
31   * -----------------------------------------------------------------------------
32   * 2001-10-31  Dennis Zikovic    Creation
33   */
34  public interface IDataSource extends java.io.Serializable
35  {
36      
37      /***
38       * Contains method that creates a new DataSource
39       */
40      public static final IDataSourceCreator DataSourceCreator = new IDataSourceCreator() {
41          /***
42           * Creates a new DataSource given an existing datasource. This sets physically up a new data source, for example
43           * by creating tables, moving data and creating relations between the tables
44           * 
45           * @param sourceDataSource The datasource with the original data
46           * @param service Target service that should be populated with thed datastructure
47           * @param copyData true if data should be copied. Otherwise, just the datastructure is created
48           * @return Returns the created datasource
49           */    
50          public IDataSource createDataSource(IDataSource sourceDataSource, IDataService service, boolean copyData)
51          {
52              if (service.getDataSourceDescriptor() != sourceDataSource.getDataSourceDescriptor())
53                  return null;
54              
55              IDataSourceDescriptor descr = service.getDataSourceDescriptor();
56              
57              IDataTransaction trans = service.newTransaction();
58              
59               // Do the create table
60               for (int i=0; i < descr.getEntityDescriptorCount(); i++) 
61               {
62                   trans.addCreate(descr.getEntityDescriptor(i));
63               }
64      
65               if (copyData)
66               {
67                   for (int i=0; i < descr.getEntityDescriptorCount(); i++) 
68                   {
69                       ISelection sel = sourceDataSource.loadSelection(descr.getEntityDescriptor(i), null);
70                       if (sel != null)
71                       {
72                           for (int j = 0; j < sel.size(); j++)
73                           {
74                               IEntity entity = sel.getEntity(j);
75                               entity.setStatusFlag(IEntity.DIRTY);
76                               entity.clearStatusFlag(IEntity.PERSISTENT);
77                               
78                               // TODO Temporary workaround for avoiding of storing images
79                               for (int k = 0; k < entity.getEntityDescriptor().getFieldCount(); k++)
80                               {
81                                   IFieldDescriptor fieldDesc = entity.getEntityDescriptor().getFieldDescriptor(k);
82                                   if (fieldDesc.getDataType().equals(DataType.IMAGE))
83                                   {
84                                       entity.setData(fieldDesc, null);
85                                   }
86                               }
87                               trans.addStore(entity);
88                           }
89                       }
90                   }   
91               }
92      
93               // Add relations (FKs)
94               for (int i=0; i < descr.getEntityDescriptorCount(); i++) 
95               {
96                   for (int j=0; j < descr.getEntityDescriptor(i).getEntityRelationCount(); j++) 
97                   {
98                       IEntityRelation relation = descr.getEntityDescriptor(i).getEntityRelation(j);
99                       if (relation.getReferenceEntityDescriptor().equals(descr.getEntityDescriptor(i)))
100                      {
101                          trans.addRelation(relation);
102                      }
103                  }
104              }
105             
106              trans.commit();
107              
108              return new SingleServiceDataSource(service);
109              
110         }
111     };
112     
113     /*** Creates a new entity and lets the data source be the manager of that 
114      * entity. This method can be used to create an entity with a data source
115      * that is not the default data source for the entitys realated 
116      * IDataSourceDescriptor.
117      */
118     public IEntity createEntity(IEntityDescriptor entityDescriptor);
119     
120     /*** Creates a new entity and lets the data source be the manager of that
121      * entity. This method can be used to create an entity with a data source
122      * that is not the default data source for the entitys realated
123      * IDataSourceDescriptor. The entity is loaded  with data from the provided 
124      * property source.
125      */
126     public IEntity createEntity(IEntityDescriptor entityDescriptor, IDataProvider propertySource);
127     
128     /*** The load method returnes a single entity object intentified by the 
129      * provided indentity qualifier that must be able to uniquely identify
130      * a single entity instance.
131      */
132     public IEntity loadEntity(IEntityDescriptor entityDescriptor, Qualifier identityQualifier);
133     
134     /*** Loads a selection of entities identyfied by the provided qualifier.
135      */
136     public ISelection loadSelection(IEntityDescriptor entityDescriptor, Qualifier qualifier);
137 
138     /*** Instucts the data source object to take over the control of an entity.
139      * The entity will henceforth belong to this data source and will have its 
140      * PERSISTENT flag reset. This method is optional and may throw an 
141      * UnsupportedOperationException.
142      */
143     public IEntity manageEntity(IEntity entity);
144     
145     /*** Returns the IDataSourceDescriptor for this data source.
146      */
147     public IDataSourceDescriptor getDataSourceDescriptor();
148     
149     /*** Returns the IDataService for this data source.
150      */
151     public IDataService getDataService();
152     
153     /***
154      * Interface used for the DataSourceCreator constant. Contains a method that can be used to copy a DataSource to 
155      * a new dataSource including migrating the database to another place
156      * 
157      * @author Klas Ehnrot
158      */
159     public interface IDataSourceCreator {
160         /***
161          * Creates a new DataSource given an existing datasource. This sets physically up a new data source, for example
162          * by creating tables, moving data and creating relations between the tables
163          * 
164          * @param sourceDataSource The datasource with the original data
165          * @param service Target service that should be populated with thed datastructure
166          * @param copyData true if data should be copied. Otherwise, just the datastructure is created
167          * @return Returns the created datasource
168          */        
169         public IDataSource createDataSource(IDataSource sourceDataSource, IDataService service, boolean copyData);
170     }    
171     
172 }
173