package com.dbxml.db.client.tools.command;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program 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.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: PermissionsBase.java,v 1.6 2004/02/12 00:17:55 bradford Exp $
 */

import com.dbxml.db.client.CollectionClient;
import com.dbxml.db.client.tools.CommandLine;
import com.dbxml.db.core.security.Access;
import com.dbxml.util.dbXMLException;
import java.io.PrintWriter;

/**
 * PermissionsBase serves as a base class for permission related Commands.
 */

public abstract class PermissionsBase extends CommandBase {
   private static final String ALL = "ALL";
   private static final String RECURSIVE = "RECURSIVE";
   private static final String[] STRS = {"NONE", "READ", "WRITE", "EXECUTE", "CREATE"};
   private static final int[] INTS = {Access.NONE, Access.READ, Access.WRITE, Access.EXECUTE, Access.CREATE};

   public void process() throws dbXMLException {
      CollectionClient col = (CollectionClient)cl.getProperty(CommandLine.COLLECTION);
      if ( col == null )
         throw new dbXMLException("Collection context required");

      String roleID = cl.getNextToken("Role ID");
      int permissions = 0;
      boolean recursive = false;

      if ( !cl.hasMoreTokens() )
         throw new dbXMLException("At least one permission must be specified");

      while ( cl.hasMoreTokens() ) {
         String mode = cl.getNextToken("Mode").toUpperCase();
         boolean found = false;
         for ( int i = 0; i < STRS.length; i++ ) {
            if ( mode.equals(STRS[i]) ) {
               permissions |= INTS[i];
               found = true;
            }
         }
         if ( mode.equals(ALL) ) {
            for ( int i = 0; i < INTS.length; i++ )
               permissions |= INTS[i];
            found = true;
         }
         if ( mode.equals(RECURSIVE) ) {
            recursive = true;
            found = true;
         }

         if ( !found )
            throw new dbXMLException("Unknown mode '"+mode+"'");
      }

      process(col, roleID, permissions, recursive);

      StringBuffer sb = new StringBuffer();
      for ( int i = 0; i < STRS.length; i++ ) {
         if ( (permissions & INTS[i]) != 0 ) {
            if ( sb.length() != 0 )
               sb.append(", ");
            sb.append(STRS[i]);
         }
      }
      String perms = sb.toString();

      sb = new StringBuffer();
      if ( recursive )
         sb.append("Recursive ");
      sb.append(getAction());
      sb.append(" Successful (");
      sb.append(perms);
      sb.append(")");

      PrintWriter pw = cl.getWriter();
      pw.println(sb.toString());
   }

   protected void process(CollectionClient col, String roleID, int permissions, boolean recursive) throws dbXMLException {
      process(col.getCanonicalName(), roleID, permissions);
      if ( recursive ) {
         String[] children = col.listCollections();
         for ( int i = 0; i < children.length; i++ ) {
            CollectionClient child = col.getCollection(children[i]);
            process(child, roleID, permissions, recursive);
         }
      }
   }

   protected abstract String getAction();

   protected abstract void process(String path, String roleID, int permissions) throws dbXMLException;
}

