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: AccessControlList.java,v 1.7 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.common.security.AccessManagerClient;
import com.dbxml.db.core.security.Access;
import com.dbxml.util.StringUtilities;
import com.dbxml.util.dbXMLException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;

/**
 * AccessControlList (ACCESS) lists the access control settings for the context
 * Collection.
 */

public final class AccessControlList extends CommandBase {
   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 path = col.getCanonicalName();
      String roleID = null;

      if ( cl.hasMoreTokens() )
         roleID = cl.getNextToken("Role ID");

      AccessManagerClient manager = new AccessManagerClient(cl.getClient());
      Map map = manager.listAccessControl(path);

      PrintWriter pw = cl.getWriter();

      if ( roleID != null ) {
         Integer i = (Integer)map.get(roleID);
         if ( i != null )
            display(pw, roleID, i.intValue());
         else
            throw new dbXMLException("Role ID '"+roleID+"' does not participate in this Collection");
      }
      else {
         System.out.println(StringUtilities.leftJustify("Role", 20)+" Permissions");
         System.out.println("-------------------- ------------------------------");

         Iterator iter = map.entrySet().iterator();
         while ( iter.hasNext() ) {
            Map.Entry entry = (Map.Entry)iter.next();
            roleID = (String)entry.getKey();
            Integer i = (Integer)entry.getValue();
            display(pw, roleID, i.intValue());
         }
      }
   }

   private void display(PrintWriter pw, String roleID, int permissions) {
      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 rights = sb.toString();

      if ( rights.length() != 0 )
         pw.println(StringUtilities.leftJustify(roleID, 20)+" "+rights);
      else
         pw.println(StringUtilities.leftJustify(roleID, 20)+" NONE");
   }
}


