AclRow.java

/*
** Module   : AclRow.java
** Abstract : AclRow model.
**
** Copyright (c) 2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- --------------------------------Description-----------------------------------
** 001 HC  20170612 Initial version.
*/
/*
** 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.admin.client.application.home.acl.model;

/**
 * Created by hc on 22.3.17.
 */

import com.goldencode.p2j.admin.*;
import java.util.*;

/**
 * A class holding all necessary info related to ACL items, to be 
 * represented in a table.
 */
public class AclRow
extends TaggedName
{
   /** Shared instance name */
   private static final String SHARED_INSTANCE = "*shared*";

   /** The string representation for the subjects list.*/
   private String subjects;

   /** A set with all subject names.*/
   private Set<String> subjectsSet;

   /** The ACL item for this row. */
   private Acl acl;

   /** The ACL parent definition for this row. */
   private AclDef aclDef;

   /**
    * Constructor.
    *
    * @param    aclDef
    *           The ACL parent definition for this row.
    * @param    acl
    *           The acl item.
    * @param    subjects
    *           The string representation for the subjects list.
    * @param    subjectsSet
    *           The set with all subject names
    */
   public AclRow(AclDef aclDef,
                 Acl acl,
                 String subjects,
                 Set<String> subjectsSet)
   {
      super((String) null, null);
      this.aclDef      = aclDef;
      this.acl         = acl;
      this.subjects    = subjects;
      this.subjectsSet = subjectsSet == null ? Collections.emptySet() : subjectsSet;
   }

   /**
    * Default ctor.
    */
   public AclRow()
   {
   }

   /**
    * Compares this instance of the class with another. The native ordering
    * is as follows:
    * <ul>
    * <li>by the instance name
    * <li>within instance by the resource type name
    * <li>within instance and resource type by the object ID
    * </ul>
    * In the effective view mode, the native order no longer applies. Since
    * the effective view is only possible for a single resource type, the
    * effective ordering is just the object ID ordering.
    *
    * @param    o
    *           Object to compare with.
    *
    * @return   the usual integers required by Comparable
    */
   @SuppressWarnings("NonJREEmulationClassesInClientCode")
   public int compareTo(TaggedName o)
   {
      AclRow row = (AclRow) o;

      if (!getInstance().equals(row.getInstance()))
      {
         return getInstance().compareTo(row.getInstance());
      }
      else if (!getResourceType().equals(row.getResourceType()))
      {
         return getResourceType().compareTo(row.getResourceType());
      }
      else
      {
         return acl.oid - ((AclRow) o).acl.oid;
      }
   }

   /**
    * Get a value for a certain column.
    *
    * @param    col
    *           The column for which the value is needed.
    *
    * @return   The content of this column.
    */
   public String getValueAt(int col)
   {
      switch (col)
      {
         case 0 :
         {
            String instance = getInstance();
            return instance.length() == 0 ? SHARED_INSTANCE
            : instance;
         }
         case 1 :
         {
            return getResourceType();
         }
         case 2 :
         {
            return "" + getId();
         }
         case 3 :
         {
            return getName();
         }
         case 4 :
         {
            return getSubjects();
         }
         case 5 :
         {
            return getRights();
         }
      }

      return null;
   }

   /**
    * Compares this instance of the class with another. Comparison is
    * case-insensitive.
    *
    * @param    o
    *           Object to compare with.
    *
    * @return   <code>true</code> when both are equal
    */
   public boolean equals(Object o)
   {
      if (!(o instanceof AclRow))
      {
         return false;
      }

      AclRow row = (AclRow) o;
      return getResourceType().equals(row.getResourceType()) &&
      getInstance().equals(row.getInstance())         &&
      getId() == row.getId() &&
      getName().equals(row.getName());
   }

   /**
    * Calculates the hash code for instances of this class. Hash codes are
    * calculated off the lower case version of the name field.
    *
    * @return   calculated integer hash code
    */
   @Override
   public int hashCode()
   {
      int result = super.hashCode();
      result = 31 * result + getResourceType().hashCode();
      result = 31 * result + getInstance().hashCode();
      result = 31 * result + getId();
      result = 31 * result + getName().hashCode();
      return result;
   }

   /**
    * Get the instance name for this item.
    *
    * @return   instance name for this item.
    */
   public String getInstance()
   {
      if (acl.origin != null)
      {
         return acl.origin;
      }

      return aclDef.server;
   }

   /**
    * Get the resource type for this item.
    *
    * @return   the resource type for this item.
    */
   public String getResourceType()
   {
      return aclDef.resourceName;
   }

   /**
    * Get the id for this item.
    *
    * @return   the id for this item.
    */
   public int getId()
   {
      return acl.oid;
   }

   /**
    * Get the Resource Instance Name for this item.
    *
    * @return   Resource Instance Name for this item.
    */
   public String getName()
   {
      return acl.name;
   }

   /**
    * Set the Resource Instance Name for this item.
    *
    * @param    name
    *           The name to be set.
    */
   public void setName(String name)
   {
      acl.name = name;
   }

   /**
    * Get the exact nature of this item.
    *
    * @return   <code>true</code> if exact; <code>false</code> if regexp.
    */
   public boolean getExact()
   {
      return acl.exact;
   }

   /**
    * Set the exact nature of this item
    *
    * @param    exact
    *           <code>true</code> if exact; <code>false</code> if regexp.
    */
   private void setExact(boolean exact)
   {
      acl.exact = exact;
   }

   /**
    * Get a string representation of the subjects.
    *
    * @return   string representation of the subjects
    */
   private String getSubjects()
   {
      return subjects;
   }

   /**
    * Get a string representation of the rights.
    *
    * @return   string representation of the rights.
    */
   private String getRights()
   {
      if (acl.rights == null)
      {
         return "*** error ***";
      }

      return acl.rights.toString();
   }

   /**
    * Acl getter.
    *
    * @return   acl object
    */
   public Acl getAcl()
   {
      return acl;
   }

   /**
    * AclDef getter.
    *
    * @return   AclDef object
    */
   public AclDef getAclDef()
   {
      return aclDef;
   }

   /**
    * Subjects getter.
    *
    * @return   subjects
    */
   public Set<String> getSubjectsSet()
   {
      return subjectsSet;
   }

   /**
    * Get the OID error flag.
    *
    * @return   <code>true</code> if OID is invalid
    */
   private boolean isErrorOid()
   {
      return acl.errOid;
   }

   /**
    * Get the resource instance name error flag.
    *
    * @return   <code>true</code> if the resource instance name is invalid
    */
   public boolean isErrorName()
   {
      return acl.errName;
   }

   /**
    * Get the resource instance name nature error flag.
    *
    * @return   <code>true</code> if the resource instance name nature is
    *           invalid
    */
   private boolean isErrorExact()
   {
      return acl.errExact;
   }

   /**
    * Get the subjects error flag.
    *
    * @return   <code>true</code> if subjects is invalid
    */
   private boolean isErrorSubjs()
   {
      return acl.errSubjs;
   }

   /**
    * Get the rights error flag.
    *
    * @return   <code>true</code> if rights is invalid
    */
   public boolean isErrorRights()
   {
      return acl.errRights;
   }

   /**
    * Clear the OID error flag.
    */
   private void clearErrorOid()
   {
      acl.errOid = false;
   }

   /**
    * Clear the resource instance name error flag.
    */
   private void clearErrorName()
   {
      acl.errName = false;
   }

   /**
    * Clear the resource instance name nature error flag.
    */
   private void clearErrorExact()
   {
      acl.errExact = false;
   }

   /**
    * Clear the subjects error flag.
    */
   private void clearErrorSubjs()
   {
      acl.errSubjs = false;
   }

   /**
    * Clear the rights error flag.
    */
   public void clearErrorRights()
   {
      acl.errRights = false;
   }
}