onetomanyfield.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 807 行 · 第 1/2 页

JAVA
807
字号
/* * 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.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;/** * Represents a field to a collection of objects where the target * hold a back-link to the source entity. */public class OneToManyField extends CollectionField {  private static final L10N L = new L10N(OneToManyField.class);  private static final Logger log     = Logger.getLogger(OneToManyField.class.getName());  private String _mapKey;  private ArrayList<String> _orderByFields;  private ArrayList<Boolean> _orderByAscending;  private ManyToOneField _sourceField;  public OneToManyField(EntityType entityType,                              String name,                              CascadeType[] cascadeTypes)    throws ConfigException  {    super(entityType, name, cascadeTypes);  }  public OneToManyField(EntityType entityType,                              String name)    throws ConfigException  {    this(entityType, name, null);  }  public OneToManyField(EntityType entityType)  {    super(entityType);  }  /**   * Sets the order by.   */  public void setOrderBy(ArrayList<String> orderByFields,                         ArrayList<Boolean> orderByAscending)  {    _orderByFields = orderByFields;    _orderByAscending = orderByAscending;  }  /**   * Returns the source type as   * entity or mapped-superclass.   */  @Override  public EntityType getEntitySourceType()  {    return (EntityType) getSourceType();  }  /**   * Returns the target type as   * entity or mapped-superclass.   */  public EntityType getEntityTargetType()  {    return (EntityType) getTargetType();  }  /**   * Returns the target type as entity.   */  @Override  public AmberType getTargetType()  {    return _sourceField.getSourceType();  }  /**   * Gets the source field.   */  public ManyToOneField getSourceField()  {    return _sourceField;  }  /**   * Sets the source field.   */  public void setSourceField(ManyToOneField sourceField)  {    _sourceField = sourceField;  }  /**   * Returns the link.   */  @Override  public LinkColumns getLinkColumns()  {    return _sourceField.getLinkColumns();  }  /**   * Gets the map key.   */  public String getMapKey()  {    return _mapKey;  }  /**   * Sets the map key.   */  public void setMapKey(String mapKey)  {    _mapKey = mapKey;  }  /**   * Initialize.   */  @Override  public void init()  {    // jpa/0gg2    if (_sourceField == null) // || getLinkColumns() == null)      throw new IllegalStateException();  }  /**   * Creates the expression for the field.   */  @Override  public AmberExpr createExpr(QueryParser parser, PathExpr parent)  {    return new OneToManyExpr(parser, parent, getLinkColumns());  }  /**   * Generates the (pre) cascade operation from   * parent to this child. This field will only   * be cascaded first if the operation can be   * performed with no risk to break FK constraints.   */  @Override  public void generatePreCascade(JavaWriter out,                                 String aConn,                                 CascadeType cascadeType)    throws IOException  {    if (cascadeType == CascadeType.PERSIST)      return;    generateInternalCascade(out, aConn, cascadeType);  }  /**   * Generates the (post) cascade operation from   * parent to this child. This field will only   * be cascaded first if the operation can be   * performed with no risk to break FK constraints.   */  @Override  public void generatePostCascade(JavaWriter out,                                  String aConn,                                  CascadeType cascadeType)    throws IOException  {    if (cascadeType != CascadeType.PERSIST)      return;    generateInternalCascade(out, aConn, cascadeType);  }  @Override  protected void generateInternalCascade(JavaWriter out,                                       String aConn,                                       CascadeType cascadeType)    throws IOException  {    if (isCascade(cascadeType)) {      String getter = "_caucho_field_" + getGetterName(); // generateSuperGetterMethod();      out.println("if (" + getter + " == null && " + generateSuperGetter("this") + " != null)");      out.pushDepth();      out.println(getSetterName() + "(" + generateSuperGetter("this") + ");");      out.popDepth();      out.println();      out.println("if (" + getter + " != null) {");      out.pushDepth();      out.print("for (Object o : " + getter);      // jpa/0v04      if (Map.class.isAssignableFrom(getJavaClass()))        out.print(".values()");      out.println(") {");      out.pushDepth();      // XXX      out.println("if (o == null)");      out.println("  continue;");      if (_sourceField != null) {        String typeName = getEntityTargetType().getJavaTypeName();        String setter = _sourceField.getSetterName();        out.println("((" + typeName + ") o)." + setter + "(this);");      }      out.print(aConn + ".");      switch (cascadeType) {      case PERSIST:        out.print("persistFromCascade");        break;      case MERGE:        out.print("merge");        break;      case REMOVE:        out.print("remove");        break;      case REFRESH:        out.print("refresh");        break;      }      out.println("(o);");      out.popDepth();      out.println("}");      out.popDepth();      out.println("}");    }  }  /**   * 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;    }    else      return index;  }  /**   * Updates from the cached copy.   */  @Override  public void generateCopyLoadObject(JavaWriter out,                                     String dst, String src,                                     int loadIndex)    throws IOException  {  }  /**   * Generates the target select.   */  @Override  public String generateTargetSelect(String id)  {    CharBuffer cb = CharBuffer.allocate();    Id key = getEntityTargetType().getId();    cb.append(key.generateSelect(id));    String value = getEntityTargetType().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 = Set.class.isAssignableFrom(getJavaClass());    boolean isMap = false;    if (!isSet) {      isMap = Map.class.isAssignableFrom(getJavaClass());    }    JType type = getJavaType();    JType []paramArgs = type.getActualTypeArguments();    JType param = paramArgs.length > 0 ? paramArgs[0] : null;    JType param2 = paramArgs.length > 1 ? paramArgs[1] : null;    out.print("protected transient ");    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";    out.print(collectionImpl);    if (param != null) {      out.print("<");      out.print(param.getPrintName());      if (isMap) {        if (param2 != null) {          out.print(", ");          out.print(param2.getPrintName());        }      }      out.print(">");    }

⌨️ 快捷键说明

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