KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > field > VersionField


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.amber.field;
31
32 import com.caucho.amber.type.RelatedType;
33 import com.caucho.amber.type.Type;
34 import com.caucho.config.ConfigException;
35 import com.caucho.java.JavaWriter;
36 import com.caucho.log.Log;
37 import com.caucho.util.CharBuffer;
38 import com.caucho.util.L10N;
39
40 import java.io.IOException JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Configuration for a bean's field
45  */

46 public class VersionField extends PropertyField {
47   private static final L10N L = new L10N(VersionField.class);
48   protected static final Logger JavaDoc log = Log.open(VersionField.class);
49
50   public VersionField(RelatedType entityType, String JavaDoc name)
51     throws ConfigException
52   {
53     super(entityType, name);
54
55     setLazy(false);
56   }
57
58   public VersionField(RelatedType entityType)
59   {
60     super(entityType);
61
62     setLazy(false);
63   }
64
65   /**
66    * Generates the is null test.
67    */

68   public String JavaDoc generateIsNull()
69   {
70     String JavaDoc getter = generateSuperGetter();
71     Type type = getColumn().getType();
72
73     return type.generateIsNull(getter);
74   }
75
76   /**
77    * Generates the increment version.
78    */

79   public void generateIncrementVersion(JavaWriter out)
80     throws IOException JavaDoc
81   {
82     int dirtyGroup = getIndex() / 64;
83     String JavaDoc dirtyVar = "__caucho_dirtyMask_" + dirtyGroup;
84
85     long dirtyMask = 1L << (getIndex() % 64);
86
87     String JavaDoc getter = generateSuperGetter();
88     Type type = getColumn().getType();
89
90     // jpa/0x02
91
out.println();
92     out.println("if (" + generateIsNull() + ")");
93     out.println(" " + generateSuperSetter("new Integer(1)") + ";");
94     out.println("else");
95     out.println(" " + generateSuperSetter(type.generateIncrementVersion(getter)) + ";");
96
97     out.println();
98     out.println("long oldMask = " + dirtyVar + ";");
99     out.println(dirtyVar + " |= " + dirtyMask + "L;");
100     out.println();
101     out.println("if (__caucho_session != null && oldMask == 0)");
102     out.println(" __caucho_session.update(this);");
103   }
104
105   /**
106    * Returns the where code
107    */

108   public String JavaDoc generateMatchArgWhere(String JavaDoc id)
109   {
110     return getColumn().generateMatchArgWhere(id);
111   }
112
113   /**
114    * Generates the post constructor initialization.
115    */

116   public void generatePostConstructor(JavaWriter out)
117     throws IOException JavaDoc
118   {
119     // jpa/0x02
120

121     String JavaDoc setter = getSetterName();
122     String JavaDoc typeName = getJavaTypeName();
123     Object JavaDoc initialValue = getColumn().getType().toObject(1);
124
125     out.println("if (" + generateIsNull() + ");");
126     out.println(" __caucho_increment_version();");
127   }
128
129   /**
130    * Generates the set version clause.
131    */

132   public void generateSetVersion(JavaWriter out,
133                                  String JavaDoc pstmt,
134                                  String JavaDoc index)
135     throws IOException JavaDoc
136   {
137     String JavaDoc value = generateGet("super");
138     Type type = getColumn().getType();
139     // jpa/0x02
140
getColumn().generateSetVersion(out, pstmt, index, value); // type.generateIncrementVersion(value));
141
}
142
143   /**
144    * Generates the update set clause
145    */

146   public void generateUpdate(CharBuffer sql)
147   {
148     sql.append(getColumn().generateUpdateSet());
149   }
150
151   /**
152    * Generates loading cache
153    */

154   public void generateUpdate(JavaWriter out,
155                              String JavaDoc maskVar,
156                              String JavaDoc pstmt,
157                              String JavaDoc index)
158     throws IOException JavaDoc
159   {
160     // jpa/0x02
161
//
162
// int group = getIndex() / 64;
163
//
164
// out.println();
165
// out.println("if (" + maskVar + "_" + group + " != 0L) {");
166
// out.pushDepth();
167

168     generateSetVersion(out, pstmt, index);
169
170     // out.popDepth();
171
// out.println("}");
172
}
173 }
174
Popular Tags