package com.dbxml.db.admin.nodes;

/*
 * 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: RoleNode.java,v 1.4 2004/02/12 00:17:54 bradford Exp $
 */

import com.dbxml.db.admin.Admin;
import com.dbxml.db.admin.dialogs.PermissionsDialog;
import com.dbxml.db.client.CollectionClient;
import com.dbxml.db.common.security.AccessManagerClient;
import com.dbxml.db.core.security.Access;
import com.dbxml.util.dbXMLException;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 * RoleNode
 */

public final class RoleNode extends AdminNode implements HasMenu, HasCollection {
   private static final int[] INTS = {Access.NONE, Access.READ, Access.WRITE, Access.EXECUTE, Access.CREATE};
   private static final String[] STRS = {"None", "Read", "Write", "Execute", "Create"};

   private static final Icon RoleIcon = new ImageIcon(RoleNode.class.getResource("role_mini.gif"));

   public static final int DATABASE = 0;
   public static final int COLLECTION = 1;
   public static final int USER = 2;

   public static final int ACTION_REMOVE_DATABASE = 0;
   public static final int ACTION_PERMISSIONS = 1;
   public static final int ACTION_REMOVE_USER = 2;

   private CollectionClient col;
   private String id;
   private String userID;
   private int type;
   private int permissions;

   public RoleNode(AdminNode parentNode, CollectionClient col, String id, int type, int permissions) {
      this(parentNode, col, id, type);
      this.permissions = permissions;
   }

   public RoleNode(AdminNode parentNode, CollectionClient col, String id, int type, String userID) {
      this(parentNode, col, id, type);
      this.userID = userID;
   }

   public RoleNode(AdminNode parentNode, CollectionClient col, String id, int type) {
      super(parentNode);

      this.col = col;
      this.id = id;
      this.type = type;
   }

   public String getLabel() {
      return id;
   }

   public String getTooltip() {
      if ( type == COLLECTION ) {
         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]);
            }
         }
         return sb.toString();
      }
      else
         return super.getTooltip();
   }

   public Icon getIcon() {
      return RoleIcon;
   }

   public CollectionClient getCollection() {
      return col;
   }

   public MenuItem[] getMenu() {
      switch ( type ) {
         case RoleNode.DATABASE:
            return new MenuItem[] {new MenuItem(ACTION_REMOVE_DATABASE, "Remove Role") };
         case RoleNode.COLLECTION:
            return new MenuItem[] {new MenuItem(ACTION_PERMISSIONS, "Modify Permissions") };
         case RoleNode.USER:
            return new MenuItem[] {new MenuItem(ACTION_REMOVE_USER, "Remove Role") };

      }
      return null;
   }

   public String getMenuTitle() {
      return "Role";
   }

   public int menuAction(int action) {
      Admin admin = Admin.getInstance();
      AccessManagerClient manager = new AccessManagerClient(col.getClient());

      switch ( action ) {
         case ACTION_REMOVE_DATABASE:
         case ACTION_REMOVE_USER:
            try {
               String title;
               String message;
               if ( type == USER ) {
                  title = "Remove Role from User";
                  message = "Are you sure you want to remove the\n"
                          + "Role '"+id+"' from User '"+userID+"'?";
               }
               else {
                  title = "Remove Role from Database";
                  message = "Are you sure you want to remove the\n"
                          + "Role '"+id+"' from the Database?";
               }
               int res = JOptionPane.showConfirmDialog(Admin.getInstance(), message, title, JOptionPane.YES_NO_OPTION);
               if ( res == JOptionPane.YES_OPTION ) {
                  switch ( type ) {
                     case DATABASE:
                        manager.removeRole(id);
                        break;
                     case USER:
                        manager.removeRoleFromUser(userID, id);
                        break;
                  }
                  return REMOVE_SELF;
               }
            }
            catch ( dbXMLException e ) {
               admin.addMessage(e.getMessage());
            }
            break;

         case ACTION_PERMISSIONS:
            PermissionsDialog perms = new PermissionsDialog(admin, col, id);
            perms.show();

            return REFRESH_PARENT;
      }

      return REFRESH_NONE;
   }
}



