manytomanyfield.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,189 行 · 第 1/3 页

JAVA
1,189
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.amber.field;import com.caucho.amber.expr.AmberExpr;import com.caucho.amber.expr.ManyToOneExpr;import com.caucho.amber.expr.OneToManyExpr;import com.caucho.amber.expr.PathExpr;import com.caucho.amber.query.QueryParser;import com.caucho.amber.table.LinkColumns;import com.caucho.amber.table.AmberTable;import com.caucho.amber.type.EntityType;import com.caucho.amber.type.AmberType;import com.caucho.bytecode.JType;import com.caucho.bytecode.JTypeWrapper;import com.caucho.config.ConfigException;import com.caucho.java.JavaWriter;import com.caucho.util.CharBuffer;import com.caucho.util.L10N;import javax.persistence.CascadeType;import java.io.IOException;import java.lang.reflect.Field;import java.util.ArrayList;import java.util.Map;import java.util.Set;import java.util.logging.Logger;/** * Configuration for a bean's field */public class ManyToManyField extends AssociationField {  private static final L10N L = new L10N(ManyToManyField.class);  private static final Logger log    = Logger.getLogger(ManyToManyField.class.getName());  private String _mapKey;  private EntityType _targetType;  private AmberTable _associationTable;  private LinkColumns _sourceLink;  private LinkColumns _targetLink;  private ArrayList<String> _orderByFields;  private ArrayList<Boolean> _orderByAscending;  public ManyToManyField(EntityType relatedType,                               String name,                               CascadeType[] cascadeTypes)    throws ConfigException  {    super(relatedType, name, cascadeTypes);  }  public ManyToManyField(EntityType relatedType,                               String name)    throws ConfigException  {    this(relatedType, name, null);  }  public ManyToManyField(EntityType relatedType)  {    super(relatedType);  }  public ManyToManyField(EntityType relatedType,                               String name,                               ManyToManyField source,                               CascadeType[] cascadeTypes)    throws ConfigException  {    super(relatedType, name, cascadeTypes);    _targetType = source.getRelatedType();    _associationTable = source._associationTable;    _sourceLink = source._targetLink;    _targetLink = source._sourceLink;  }  /**   * Gets the map key.   */  public String getMapKey()  {    return _mapKey;  }  /**   * Sets the map key.   */  public void setMapKey(String mapKey)  {    _mapKey = mapKey;  }  /**   * Sets the target type.   */  @Override  public void setType(AmberType targetType)  {    _targetType = (EntityType) targetType;    super.setType(targetType);  }  /**   * Returns the source type as   * entity or mapped-superclass.   */  public EntityType getRelatedType()  {    return (EntityType) getSourceType();  }  /**   * Returns the target type.   */  @Override  public EntityType getTargetType()  {    return _targetType;  }  /**   * Returns the association table   */  public AmberTable getAssociationTable()  {    return _associationTable;  }  /**   * Sets the association table   */  public void setAssociationTable(AmberTable table)  {    _associationTable = table;  }  /**   * Adds a column from the association table to the source side.   */  public void setSourceLink(LinkColumns link)  {    _sourceLink = link;  }  /**   * Returns the source link.   */  public LinkColumns getSourceLink()  {    return _sourceLink;  }  /**   * Adds a column from the association table to the target side.   */  public void setTargetLink(LinkColumns link)  {    _targetLink = link;  }  /**   * Returns the target link.   */  public LinkColumns getTargetLink()  {    return _targetLink;  }  /**   * Sets the order by.   */  public void setOrderBy(ArrayList<String> orderByFields,                         ArrayList<Boolean> orderByAscending)  {    _orderByFields = orderByFields;    _orderByAscending = orderByAscending;  }  /**   * Initializes the field.   */  @Override  public void init()    throws ConfigException  {    // XXX: might not have cascade delete if there's an associated entity    _targetLink.setSourceCascadeDelete(true);    _sourceLink.setSourceCascadeDelete(true);  }  /**   * Generates the set clause.   */  @Override  public void generateStatementSet(JavaWriter out, String pstmt,                          String obj, String index)    throws IOException  {  }  /**   * Generates the select clause.   */  @Override  public String generateLoadSelect(String id)  {    return null;  }  /**   * Generates loading code after the basic fields.   */  @Override  public int generatePostLoadSelect(JavaWriter out, int index)    throws IOException  {    if (! isLazy()) {      out.println(getGetterName() + "();");    }    return ++index;  }  /**   * Creates the expression for the field.   */  @Override  public AmberExpr createExpr(QueryParser parser, PathExpr parent)  {    return new ManyToOneExpr(new OneToManyExpr(parser, parent, _sourceLink),                             _targetLink);  }  /**   * Updates from the cached copy.   */  @Override  public void generateCopyLoadObject(JavaWriter out,                                     String dst, String src,                                     int loadIndex)    throws IOException  {    // jpa/0s2j    if (dst.equals("item"))      return;    String var = "_caucho_field_" + getGetterName();    // order matters: jpa/0s2k    String value = var; // generateGet(src);    out.println(generateSet(dst, value) + ";");    out.println(generateAccessor(dst, var) + " = " + generateAccessor(src, var) + ";");    if (! dst.equals("super")) { // || isLazy())) {      String oThis = "((" + getRelatedType().getInstanceClassName() + ") " + dst + ")";      out.println(generateSuperSetter(oThis, generateSuperGetter("this")) + ";");    }  }  /**   * Updates the cached copy.   */  @Override  public void generateMergeFrom(JavaWriter out,                                      String dst, String src)    throws IOException  {    // jpa/0s2k    int updateIndex = 0;    generateCopyLoadObject(out, dst, src, updateIndex);    out.println();    // jpa/0i61    generatePreCascade(out, "aConn", CascadeType.MERGE);  }  /**   * Generates the target select.   */  @Override  public String generateTargetSelect(String id)  {    return getTargetType().getId().generateSelect(id);  }  /**   * Generates the select clause.   */  public String generateTargetLoadSelect(String id)  {    CharBuffer cb = CharBuffer.allocate();    cb.append(getTargetType().getId().generateLoadSelect(id));    String value = getTargetType().generateLoadSelect(id);    if (cb.length() > 0 && value.length() > 0)      cb.append(", ");    cb.append(value);    return cb.close();  }  /**   * Generates the set property.   */  @Override  public void generateGetterMethod(JavaWriter out)    throws IOException  {    String var = "_caucho_field_" + getGetterName();    boolean isSet = getJavaType().isAssignableTo(Set.class);    boolean isMap = false;    if (!isSet) {      isMap = getJavaType().isAssignableTo(Map.class);    }    JType type = getJavaType();    JType []paramArgs = type.getActualTypeArguments();    JType param = paramArgs.length > 0 ? paramArgs[0] : null;    JType param2 = paramArgs.length > 1 ? paramArgs[1] : null;    // jpa/0l44    String s = "protected ";    String collectionImpl;    if (isSet)      collectionImpl = "com.caucho.amber.collection.SetImpl";    else if (isMap)      collectionImpl = "com.caucho.amber.collection.MapImpl";    else      collectionImpl = "com.caucho.amber.collection.CollectionImpl";    s = s + collectionImpl;    if (param != null) {      s = s + '<' + param.getPrintName();      if (isMap) {        if (param2 != null) {          s = s + ", " + param2.getPrintName();        }      }      s = s + '>';    }    s = s + " " + var;    // jpa/0i5g    out.println("java.util.HashSet<" + getTargetType().getBeanClass().getName() + "> " + var + "_added;");    out.println();    out.println(s + ';');    out.println();    out.println("public " + getJavaTypeName() + " " + getGetterName() + "()");    out.println("{");    out.pushDepth();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?