001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.amber.field;
031:
032: import com.caucho.amber.type.RelatedType;
033: import com.caucho.amber.type.Type;
034: import com.caucho.config.ConfigException;
035: import com.caucho.java.JavaWriter;
036: import com.caucho.log.Log;
037: import com.caucho.util.CharBuffer;
038: import com.caucho.util.L10N;
039:
040: import java.io.IOException;
041: import java.util.logging.Logger;
042:
043: /**
044: * Configuration for a bean's field
045: */
046: public class VersionField extends PropertyField {
047: private static final L10N L = new L10N(VersionField.class);
048: protected static final Logger log = Log.open(VersionField.class);
049:
050: public VersionField(RelatedType entityType, String name)
051: throws ConfigException {
052: super (entityType, name);
053:
054: setLazy(false);
055: }
056:
057: public VersionField(RelatedType entityType) {
058: super (entityType);
059:
060: setLazy(false);
061: }
062:
063: /**
064: * Generates the is null test.
065: */
066: public String generateIsNull() {
067: String getter = generateSuperGetter();
068: Type type = getColumn().getType();
069:
070: return type.generateIsNull(getter);
071: }
072:
073: /**
074: * Generates the increment version.
075: */
076: public void generateIncrementVersion(JavaWriter out)
077: throws IOException {
078: int dirtyGroup = getIndex() / 64;
079: String dirtyVar = "__caucho_dirtyMask_" + dirtyGroup;
080:
081: long dirtyMask = 1L << (getIndex() % 64);
082:
083: String getter = generateSuperGetter();
084: Type type = getColumn().getType();
085:
086: // jpa/0x02
087: out.println();
088: out.println("if (" + generateIsNull() + ")");
089: out.println(" " + generateSuperSetter("new Integer(1)") + ";");
090: out.println("else");
091: out.println(" "
092: + generateSuperSetter(type
093: .generateIncrementVersion(getter)) + ";");
094:
095: out.println();
096: out.println("long oldMask = " + dirtyVar + ";");
097: out.println(dirtyVar + " |= " + dirtyMask + "L;");
098: out.println();
099: out.println("if (__caucho_session != null && oldMask == 0)");
100: out.println(" __caucho_session.update(this);");
101: }
102:
103: /**
104: * Returns the where code
105: */
106: public String generateMatchArgWhere(String id) {
107: return getColumn().generateMatchArgWhere(id);
108: }
109:
110: /**
111: * Generates the post constructor initialization.
112: */
113: public void generatePostConstructor(JavaWriter out)
114: throws IOException {
115: // jpa/0x02
116:
117: String setter = getSetterName();
118: String typeName = getJavaTypeName();
119: Object initialValue = getColumn().getType().toObject(1);
120:
121: out.println("if (" + generateIsNull() + ");");
122: out.println(" __caucho_increment_version();");
123: }
124:
125: /**
126: * Generates the set version clause.
127: */
128: public void generateSetVersion(JavaWriter out, String pstmt,
129: String index) throws IOException {
130: String value = generateGet("super");
131: Type type = getColumn().getType();
132: // jpa/0x02
133: getColumn().generateSetVersion(out, pstmt, index, value); // type.generateIncrementVersion(value));
134: }
135:
136: /**
137: * Generates the update set clause
138: */
139: public void generateUpdate(CharBuffer sql) {
140: sql.append(getColumn().generateUpdateSet());
141: }
142:
143: /**
144: * Generates loading cache
145: */
146: public void generateUpdate(JavaWriter out, String maskVar,
147: String pstmt, String index) throws IOException {
148: // jpa/0x02
149: //
150: // int group = getIndex() / 64;
151: //
152: // out.println();
153: // out.println("if (" + maskVar + "_" + group + " != 0L) {");
154: // out.pushDepth();
155:
156: generateSetVersion(out, pstmt, index);
157:
158: // out.popDepth();
159: // out.println("}");
160: }
161: }
|