KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > jdo > SequenceGenerator


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  */

25
26 package org.objectweb.speedo.generation.jdo;
27
28 import java.util.Iterator JavaDoc;
29
30 import org.objectweb.speedo.api.SpeedoException;
31 import org.objectweb.speedo.api.SpeedoProperties;
32 import org.objectweb.speedo.generation.lib.AbstractGeneratorComponent;
33 import org.objectweb.speedo.metadata.SpeedoClass;
34 import org.objectweb.speedo.metadata.SpeedoExtension;
35 import org.objectweb.speedo.metadata.SpeedoIdentity;
36 import org.objectweb.speedo.metadata.SpeedoPackage;
37 import org.objectweb.speedo.sequence.lib.SpeedoSequence;
38 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor;
39
40 /**
41  * @author Y.Bersihand
42  */

43 public class SequenceGenerator extends AbstractGeneratorComponent {
44
45     public final static String JavaDoc LOGGER_NAME
46             = SpeedoProperties.LOGGER_NAME + ".generation.jorm";
47
48     public boolean init() throws SpeedoException {
49         if (scp.getXmldescriptor().isEmpty()) {
50             return false;
51         }
52         logger = scp.loggerFactory.getLogger(LOGGER_NAME);
53         return true;
54     }
55
56     public void process() throws SpeedoException {
57         if (scp.getXmldescriptor().isEmpty()) {
58             return;
59         }
60         //add the sequence meta information to serialize for each xml descriptor
61
for (Iterator JavaDoc itDesc = scp.getXmldescriptor().values().iterator(); itDesc.hasNext();) {
62             addSerializeSequence((SpeedoXMLDescriptor) itDesc.next());
63         }
64     }
65     
66     private void addSerializeSequence(SpeedoXMLDescriptor desc)
67     throws SpeedoException {
68         // Calculate the list of the Jorm Meta Object which must be serialized
69
for (Iterator JavaDoc itPack = desc.jdoPackage.values().iterator(); itPack.hasNext();) {
70             SpeedoPackage sp = (SpeedoPackage) itPack.next();
71             for (Iterator JavaDoc itSequence = sp.jdoSequence.values().iterator(); itSequence.hasNext();) {
72                 SpeedoSequence jdoSequence = (SpeedoSequence) itSequence.next();
73                 desc.mos.add(jdoSequence);
74             }
75             for (Iterator JavaDoc itClass = sp.jdoClass.values().iterator(); itClass.hasNext();) {
76                 SpeedoClass speedoClass = (SpeedoClass) itClass.next();
77                 //if the identityType is datastore and id extension is sequence
78
if (speedoClass.identityType == SpeedoIdentity.CONTAINER_ID
79                         && speedoClass.getExtension(SpeedoProperties.VENDOR_NAME,
80                             SpeedoProperties.ID).value.equals(SpeedoProperties.ID_SEQUENCE)) {
81                     //create a new sequence
82
SpeedoSequence newSequence = new SpeedoSequence();
83                     String JavaDoc name;
84                     //sequence name
85
SpeedoExtension se = speedoClass.getExtension(
86                             SpeedoProperties.VENDOR_NAME, SpeedoProperties.SQL_SEQ_NAME);
87                     if (se != null) {
88                         name = se.value;
89                     } else {
90                         se = speedoClass.getExtension(
91                             SpeedoProperties.VENDOR_NAME, SpeedoProperties.SQL_NAME);
92                         if (se == null) {
93                             name = speedoClass.name.toUpperCase();
94                         } else {
95                             name = se.value;
96                         }
97                         name += "_SEQ";
98                     }
99                     newSequence.name = name;
100                     newSequence.datastoreName = name;
101                     
102                     //sequence increment
103
se = speedoClass.getExtension(
104                             SpeedoProperties.VENDOR_NAME, SpeedoProperties.SQL_SEQ_INC);
105                     if (se != null) {
106                         //set the increment
107
newSequence.increment = new Integer JavaDoc(se.value);
108                     } else {
109                         newSequence.increment = new Integer JavaDoc(1);
110                     }
111
112                     // sequence start
113
se = speedoClass.getExtension(
114                             SpeedoProperties.VENDOR_NAME, SpeedoProperties.SQL_SEQ_START);
115                     if (se != null) {
116                         //set the start
117
newSequence.start = new Integer JavaDoc(se.value);
118                     }
119
120                     //sequence start
121
se = speedoClass.getExtension(
122                             SpeedoProperties.VENDOR_NAME, SpeedoProperties.SQL_SEQ_CACHE);
123                     if (se != null) {
124                         //set the cache
125
newSequence.cache = new Integer JavaDoc(se.value);
126                     }
127
128                     // sequence allocator
129
se = speedoClass.getExtension(
130                             SpeedoProperties.VENDOR_NAME, SpeedoProperties.SQL_SEQ_ALLOCATOR);
131                     if (se != null) {
132                         //set the factory class
133
newSequence.factoryClass = se.value;
134                     }
135                     //set the package name
136
newSequence.packageName = sp.name;
137                     //set a default strategy
138
newSequence.strategy = SpeedoSequence.NON_TRANSACTIONAL;
139                     //add the sequence to the package
140
sp.addSequence(newSequence);
141                     //add it to the list of sequences to serialize
142
desc.mos.add(newSequence);
143                 }
144             }
145         }
146     }
147 }
148
Popular Tags