DsRelationDefinition.java

/*
** Module   : DsRelationDefinition.java
** Abstract : A container for a DataSet Table metadata.
**
** Copyright (c) 2019-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- ---------------------------------------Description---------------------------------------
** 001 OM  20130708 Created initial version.
**     OM  20190716 Added missing Javadocs. Added missing RELATION attributes.
**     CA  20190812 Added toString().
** 002 IAS 20200908 Rework (de)serialization.
** 003 IAS 20200922 Get rid of possible NPE on serialization.
**     OM  20201231 DataRelation API change. Replaced multiple string concatenation with StringBuilder.
** 004 HC  20230116 Replaced some handle usages with the actual wrapped resources for
**                  performance.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program 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.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
** 
** Additional terms under GNU Affero GPL version 3 section 7:
** 
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
** 
**   0. Attribution Requirement.
** 
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
** 
**   1. No License To Use Trademarks.
** 
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
** 
**   2. No Misrepresentation of Affiliation.
** 
**     You may not represent yourself as Golden Code Development Corporation or FWD.
** 
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
** 
**   3. No Misrepresentation of Source or Origin.
** 
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/

package com.goldencode.p2j.persist;

import static com.goldencode.util.NativeTypeSerializer.*;

import java.io.*;

/**
 * Container for a relation's metadata: name, buffers and where-string. Used to send the metadata
 * of a relation to a remote side, when remote appserver calls are involved.
 */
public class DsRelationDefinition
implements Externalizable
{
   /** The name of this relation. */
   private String name;
   
   /** The name of the parent buffer. */
   private String parentBuffer;
   
   /** The name of the child buffer. */
   private String childBuffer;
   
   /** The WHERE-STRING string. */
   private String whereString;
   
   /** {@code true} if the relation is active. */
   private boolean active;
   
   /** {@code true} if this is a parent-id relation type. */
   private boolean parentId;
   
   /** The list of relation fields for this relation. */
   private String pairs;
   
   /** The FOREIGN-KEY-HIDDEN status of this relation. */
   private boolean fkeyHidden;
   
   /** {@code true} to flag this as a RECURSIVE relation. */
   private boolean recursive;
   
   /** The NESTED status of this relation. */
   private boolean nested;
   
   /** The REPOSITION status of this relation. */
   private boolean reposition;
   
   /**
    * Default c'tor, explicitly added to allow instances of this class to be created on 
    * deserialization. Not for public use.
    */
   public DsRelationDefinition()
   {
   }
   
   /**
    * Constructor used by {@link DataSetContainer#DataSetContainer}.
    * 
    * @param   rel
    *          The {@code DataRelation} this object will be defining. 
    */
   public DsRelationDefinition(DataRelation rel)
   {
      this.name = rel.name().toStringMessage();
      this.parentBuffer = rel.getParentBufferNative().name().toStringMessage();
      this.childBuffer = rel.getChildBufferNative().name().toStringMessage();
      this.whereString = rel.getWhereString().toStringMessage();
      this.active = rel._isActive();
      this.parentId = rel.isParentIdRelation().booleanValue();
      this.pairs = rel._getRelationFields();
      this.reposition = rel._isReposition();
      this.nested = rel.isNested().getValue();
      this.recursive = rel.isRecursive().getValue();
      this.fkeyHidden = rel.getForeignKeyHidden().getValue();
   }
   
   /**
    * Gets the name of this relation.
    * 
    * @return   See above.
    */
   public String getName()
   {
      return name;
   }
   
   /**
    * Sets the name of this relation.
    *
    * @param   name
    *          The (new) name of the relation.
    */
   public void setName(String name)
   {
      this.name = name;
   }
   
   /**
    * Gets the name of the buffer with parent role in this relation.
    * 
    * @return  the buffer with parent role in this relation.
    */
   public String getParentBuffer()
   {
      return parentBuffer;
   }
   
   /**
    * Configures the buffer with parent role in this relation.
    *
    * @param   parentBuffer 
    *          the name of the (new) buffer with parent role in this relation.
    */
   public void setParentBuffer(String parentBuffer)
   {
      this.parentBuffer = parentBuffer;
   }
   
   /**
    * Retrieves the name of the buffer with child role in this relation.
    *
    * @return  the buffer with child role in this relation.
    */
   public String getChildBuffer()
   {
      return childBuffer;
   }
   
   /**
    * Configures the buffer with child role in this relation.
    *
    * @param   childBuffer
    *          the name of the (new) buffer with child role in this relation.
    */
   public void setChildBuffer(String childBuffer)
   {
      this.childBuffer = childBuffer;
   }
   
   /**
    * Obtain the {@code WHERE-STRING} predicate of this relation.
    * 
    * @return  the {@code WHERE-STRING} predicate of this relation.
    */
   public String getWhereString()
   {
      return whereString;
   }
   
   /**
    * Configures the {@code WHERE-STRING} predicate of this relation.
    *
    * @param   whereString
    *          The {@code WHERE-STRING} predicate of this relation.
    */
   public void setWhereString(String whereString)
   {
      this.whereString = whereString;
   }
   
   /**
    * Set the field pairs of this relation.
    *
    * @param   pairs
    *          The field pairs of this relation.
    */
   public void setFieldPairs(String pairs)
   {
      this.pairs = pairs;
   }
   
   /**
    * Set the FOREIGN-KEY-HIDDEN status of this relation.
    *
    * @param   fkeyHidden
    *          The FOREIGN-KEY-HIDDEN status of this relation.
    */
   public void setFkeyHidden(boolean fkeyHidden)
   {
      this.fkeyHidden = fkeyHidden;
   }
   
   /**
    * Set the RECURSIVE status of this relation.
    *
    * @param   recursive
    *          The RECURSIVE status of this relation.
    */
   public void setRecursive(boolean recursive)
   {
      this.recursive = recursive;
   }
   
   /**
    * Set the NESTED status of this relation.
    *
    * @param   nested
    *          The NESTED status of this relation.
    */
   public void setNested(boolean nested)
   {
      this.nested = nested;
   }
   
   /**
    * Set the REPOSITION status of this relation.
    *
    * @param   reposition
    *          The REPOSITION status of this relation.
    */
   public void setReposition(boolean reposition)
   {
      this.reposition = reposition;
   }
   
   /**
    * Checks whether this relation is active.
    * 
    * @return  {@code true} when the relation is active.
    */
   public boolean isActive()
   {
      return active;
   }
   
   /**
    * Sets the status of this relation.
    *
    * @param   active
    *          Use {@code true} to set the relation as active and {@code false} otherwise.
    */
   public void setActive(boolean active)
   {
      this.active = active;
   }
   
   /**
    * Check whether this is a PARENT-ID relation.
    * 
    * @return  {@code true} if this is a PARENT-ID relation.
    */
   public boolean isParentId()
   {
      return parentId;
   }
   
   /**
    * Get the field pairs of this relation.
    * 
    * @return  the field pairs of this relation.
    */
   public String getFieldPairs()
   {
      return pairs;
   }
   
   /**
    * Get the FOREIGN-KEY-HIDDEN status of this relation.
    *
    * @return  the FOREIGN-KEY-HIDDEN status of this relation.
    */
   public boolean isFkeyHidden()
   {
      return fkeyHidden;
   }
   
   /**
    * Get the RECURSIVE status of this relation.
    *
    * @return  the RECURSIVE status of this relation.
    */
   public boolean isRecursive()
   {
      return recursive;
   }
   
   /**
    * Get the NESTED status of this relation.
    *
    * @return  the NESTED status of this relation.
    */
   public boolean isNested()
   {
      return nested;
   }
   
   /**
    * Get the REPOSITION status of this relation.
    *
    * @return  the REPOSITION status of this relation.
    */
   public boolean isReposition()
   {
      return reposition;
   }
   
   /**
    * Configures whether this is a PARENT-ID relation.
    *
    * @param   parentId
    *          Use {@code true} to set this relation as a PARENT-ID relation.
    */
   public void setParentId(boolean parentId)
   {
      this.parentId = parentId;
   }
   
   /**
    * Send the relation definition to the specified output destination.
    * 
    * @param    out
    *           The output destination to which the relation definition will be sent.
    *
    * @throws   IOException
    *           In case of I/O errors.
    */
   public final void writeExternal(ObjectOutput out)
   throws IOException
   {
      writeString(out, name);
      writeString(out,parentBuffer);
      writeString(out,childBuffer);
      writeString(out,whereString);
      out.writeByte(active ? 1 : 0);
      out.writeByte(parentId ? 1 : 0);
      writeString(out,pairs);
      out.writeByte(fkeyHidden ? 1 : 0);
      out.writeByte(recursive ? 1 : 0);
      out.writeByte(nested ? 1 : 0);
      out.writeByte(reposition ? 1 : 0);
   }
   
   /**
    * Read the relation definition from the specified input source.
    * 
    * @param   in
    *          The input source from which the relation definition will be read.
    *
    * @throws  IOException
    *          In case of I/O errors.
    */
   public final void readExternal(ObjectInput in)
   throws IOException
   {
      name = readString(in);
      parentBuffer = readString(in);
      childBuffer = readString(in);
      whereString = readString(in);
      active = in.readByte() == 1;
      parentId = in.readByte() == 1;
      pairs = readString(in);
      fkeyHidden = in.readByte() == 1;
      recursive = in.readByte() == 1;
      nested = in.readByte() == 1;
      reposition = in.readByte() == 1;
   }
   
   /**
    * Get a string representation of this relation.
    * 
    * @return    See above.
    */
   @Override
   public String toString()
   {
      StringBuilder sb = new StringBuilder();
      sb.append(name).append(": ").append(parentBuffer).append(" has child ").append(childBuffer)
        .append("\n")
        .append("\twhereString: ").append(whereString)
        .append("\tpairs: ").append(pairs)
        .append("\tactive: ").append(active)
        .append("\tparentId: ").append(parentId)
        .append("\tfkeyHidden: ").append(fkeyHidden)
        .append("\trecursive: ").append(recursive)
        .append("\tnested: ").append(nested)
        .append("\treposition: ").append(reposition);
      
      return sb.toString();
   }
}