/*
 * Licensed to the Sakai Foundation (SF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The SF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package org.sakaiproject.kernel.user;

import com.google.inject.Inject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sakaiproject.kernel.api.Registry;
import org.sakaiproject.kernel.api.RegistryService;
import org.sakaiproject.kernel.api.jcr.JCRConstants;
import org.sakaiproject.kernel.api.jcr.support.JCRNodeFactoryService;
import org.sakaiproject.kernel.api.jcr.support.JCRNodeFactoryServiceException;
import org.sakaiproject.kernel.api.user.UserFactoryService;
import org.sakaiproject.kernel.api.user.UserProvisionAgent;
import org.sakaiproject.kernel.api.userenv.UserEnvironment;
import org.sakaiproject.kernel.util.ISO9075;
import org.sakaiproject.kernel.util.JcrUtils;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.query.Query;

public class InboxAgent implements UserProvisionAgent {

  Log log = LogFactory.getLog(InboxAgent.class);

  private static final String KEY = "InboxAgent";
  private static final int PRIORITY = 0;

  // FIXME Get these strings from somewhere instead of hardcoding them
  private static final String INBOX_LABEL = "inbox";
  private static final String INBOX_PATH = "messages/inbox";

  private UserFactoryService userFactoryService = null;
  private JCRNodeFactoryService nodeFactory = null;

  @Inject
  public InboxAgent(RegistryService registryService,
      UserFactoryService userFactoryService, JCRNodeFactoryService nodeFactory) {
    this.userFactoryService = userFactoryService;
    this.nodeFactory = nodeFactory;
    Registry<String, UserProvisionAgent> registry = registryService
        .getRegistry(UserProvisionAgent.REGISTRY);
    registry.add(this);
  }

  public void provision(UserEnvironment userEnv) {
    String path = "/" + ISO9075.encodePath(userFactoryService
        .getUserPrivatePath(userEnv.getUser().getUuid())
        + INBOX_PATH);
    String query = "/" + path + "/element(*, " + JCRConstants.NT_FILE + ")[@"
        + JCRConstants.JCR_LABELS + "='" + INBOX_LABEL + "']";
    try {
      Node inbox = nodeFactory.createFolder(path);
      JcrUtils.makeSmartNode(inbox, Query.XPATH, query);
    } catch (JCRNodeFactoryServiceException e) {
      log.error(e);
    } catch (RepositoryException e) {
      log.error(e);
    }
  }

  public String getKey() {
    return KEY;
  }

  public int getPriority() {
    return PRIORITY;
  }

}

@@ -1963,30 +1963,96 @@
 ing 
-INBOX_PATH = %22messages
+DELIVERY_ROOT = %22messages%22;%0A  private static final String INBOX_PATH = DELIVERY_ROOT + %22
 /inb
@@ -2618,33 +2618,8 @@
 h = 
-%22/%22 + ISO9075.encodePath(
 user
@@ -2628,33 +2628,24 @@
 ctoryService
-%0A        
 .getUserPriv
@@ -2669,16 +2669,25 @@
 etUser()
+%0A        
 .getUuid
@@ -2693,65 +2693,97 @@
 d())
-%0A        + INBOX_PATH);%0A    String query = %22/%22 + path + %22
+;%0A    String query = %22/jcr:root/%22 + ISO9075.encodePath(path + DELIVERY_ROOT)%0A        + %22/
 /ele
@@ -2791,16 +2791,24 @@
 ent(*, %22
+%0A       
  + JCRCo
@@ -2831,19 +2831,17 @@
 + %22)
-%5B@%22%0A       
+%22; // %5B@%22
  + J
@@ -2871,16 +2871,57 @@
 + %22='%22 +
+%0A                                      //
  INBOX_L
@@ -2991,16 +2991,29 @@
 der(path
+ + INBOX_PATH
 );%0A     

Fix query to properly get the inbox