/* Copyright 2001 The JA-SIG Collaborative.  All rights reserved.
*  See license distributed with this file and
*  available online at http://www.uportal.org/license.html
*/

package  org.jasig.portal.channels.permissionsmanager.commands;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;

import org.jasig.portal.channels.permissionsmanager.CommandFactory;
import org.jasig.portal.channels.permissionsmanager.IPermissionCommand;
import org.jasig.portal.channels.permissionsmanager.PermissionsSessionData;
import org.jasig.portal.security.IPermission;
import org.jasig.portal.security.IUpdatingPermissionManager;
import org.jasig.portal.services.AuthorizationService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;


/**
 * An IPermissionCommand implementation that processes form data from the
 * CPermissionsManager matrix screen and records all permissions
 *
 * @author Alex Vigdor
 * @version $Revision$
 */
public class AssignPermissions
        implements IPermissionCommand {
    private static final Log log = LogFactory.getLog(AssignPermissions.class);
    
    /** Creates new AssignPermissions */
    public AssignPermissions () {
    }

    /*
     *  expects to receive all permissions in form
     *  key['permission//{owner}|{principal}|{activity}|{target}'] = value['INHERIT','GRANT','DENY']
     */
    public void execute (PermissionsSessionData session) throws Exception {
        
            log.debug("PermissionsManager->AssignPermissions processing");
            Element root = session.XML.getDocumentElement();
            Enumeration formkeys = session.runtimeData.getParameterNames();
            HashMap owners = new HashMap();
            while (formkeys.hasMoreElements()) {
                String key = (String)formkeys.nextElement();
                if (key.indexOf("permission//") == 0) {
                    PermissionHolder ph = new PermissionHolder();
                    String split1 = key.substring(12);
                    ph.owner = split1.substring(0, split1.indexOf("|"));
                    String split2 = split1.substring(split1.indexOf("|") +
                            1);
                    ph.principal = split2.substring(0, split2.indexOf("|"));
                    String split3 = split2.substring(split2.indexOf("|") +
                            1);
                    ph.activity = split3.substring(0, split3.indexOf("|"));
                    ph.target = split3.substring(split3.indexOf("|") + 1);
                    ph.type = session.runtimeData.getParameter(key);
                    log.debug("Processing "
                            + ph.type + " permission o=" + ph.owner + " p="
                            + ph.principal + " a=" + ph.activity + " t=" +
                            ph.target);
                    if (!owners.containsKey(ph.owner)) {
                        owners.put(ph.owner, new ArrayList());
                    }
                    ((ArrayList)owners.get(ph.owner)).add(ph);
                }
            }
            String[] ownerkeys = (String[])owners.keySet().toArray(new String[0]);
            for (int i = 0; i < ownerkeys.length; i++) {
                String owner = ownerkeys[i];
                IUpdatingPermissionManager upm = AuthorizationService.instance().newUpdatingPermissionManager(owner);
                ArrayList phs = (ArrayList)owners.get(owner);
                IPermission[] ipsd = pHolder2DeleteArray(upm, phs);
                log.debug("removing " + String.valueOf(ipsd.length)
                        + " old permissions");
                upm.removePermissions(ipsd);
                IPermission[] ipsa = pHolder2AddArray(upm, phs);
                log.debug("adding " + String.valueOf(ipsa.length)
                        + " new permissions");
                upm.addPermissions(ipsa);
            }
            IPermissionCommand wrapit = CommandFactory.get("Cancel");
            wrapit.execute(session);
        
    }

    private class PermissionHolder {
        String owner;
        String principal;
        String activity;
        String target;
        String type;
    }

    private IPermission[] pHolder2DeleteArray (IUpdatingPermissionManager upm,
            ArrayList holders) {
        ArrayList rlist = new ArrayList();
        for (int i = 0; i < holders.size(); i++) {
            try {
                PermissionHolder ph = (PermissionHolder)holders.get(i);
                IPermission p = upm.newPermission(null);
                p.setPrincipal(ph.principal);
                p.setActivity(ph.activity);
                p.setTarget(ph.target);
                rlist.add(p);
            } catch (Exception e) {
                log.error(e, e);
            }
        }
        return  (IPermission[])rlist.toArray(new IPermission[0]);
    }

    private IPermission[] pHolder2AddArray (IUpdatingPermissionManager upm,
            ArrayList holders) {
        ArrayList rlist = new ArrayList();
        for (int i = 0; i < holders.size(); i++) {
            try {
                PermissionHolder ph = (PermissionHolder)holders.get(i);
                IPermission p = upm.newPermission(null);
                p.setPrincipal(ph.principal);
                p.setActivity(ph.activity);
                p.setTarget(ph.target);
                if (ph.type.equals("GRANT")) {
                    p.setType("GRANT");
                    rlist.add(p);
                }
                else if (ph.type.equals("DENY")) {
                    p.setType("DENY");
                    rlist.add(p);
                }
            } catch (Exception e) {
                log.error(e);
            }
        }
        return  (IPermission[])rlist.toArray(new IPermission[0]);
    }
}




@@ -1491,25 +1491,58 @@
 n %7B%0A        
-%0A
+    if (log.isDebugEnabled())%0A    
             
@@ -2652,24 +2652,74 @@
 meter(key);%0A
+                    if (log.isDebugEnabled())%0A    
             
@@ -2750,16 +2750,16 @@
 ssing %22%0A
-
         
@@ -3603,32 +3603,78 @@
 rray(upm, phs);%0A
+                if (log.isDebugEnabled())%0A    
                 
@@ -3717,32 +3717,32 @@
 Of(ipsd.length)%0A
-
                 
@@ -3874,32 +3874,78 @@
 rray(upm, phs);%0A
+                if (log.isDebugEnabled())%0A    
                 
@@ -3986,32 +3986,32 @@
 Of(ipsa.length)%0A
-
                 
@@ -5849,32 +5849,32 @@
 (Exception e) %7B%0A
-
                 
@@ -5883,16 +5883,19 @@
 g.error(
+e, 
 e);%0A    

conditionalized logging below Error level. fixed one instance of log.error(e) to be log.error(e,e);