/*
 *  Copyright (C) 2012 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW 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 Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

package org.waterforpeople.mapping.app.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import com.gallatinsystems.common.Constants;
import com.gallatinsystems.user.dao.UserAuthorizationDAO;
import com.gallatinsystems.user.dao.UserDao;
import com.gallatinsystems.user.dao.UserRoleDao;
import com.gallatinsystems.user.domain.Permission;
import com.gallatinsystems.user.domain.User;
import com.gallatinsystems.user.domain.UserAuthorization;
import com.gallatinsystems.user.domain.UserRole;
import com.google.appengine.api.users.UserServiceFactory;

public class CurrentUserServlet extends HttpServlet {

    private UserRoleDao userRoleDAO = new UserRoleDao();

    private UserAuthorizationDAO userAuthorizationDAO = new UserAuthorizationDAO();

    private static final long serialVersionUID = -430515593814261770L;
    private static final Logger log = Logger.getLogger(CurrentUserServlet.class
            .getName());

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        final VelocityEngine engine = new VelocityEngine();
        engine.setProperty("runtime.log.logsystem.class",
                "org.apache.velocity.runtime.log.NullLogChute");
        try {
            engine.init();
        } catch (Exception e) {
            log.log(Level.SEVERE, "Could not initialize velocity", e);
        }

        Template t = null;
        try {
            t = engine.getTemplate("CurrentUser.vm");
        } catch (Exception e) {
            log.log(Level.SEVERE, "Could not get the template `CurrentUser`", e);
            return;
        }

        final VelocityContext context = new VelocityContext();
        final UserDao uDao = new UserDao();
        final String currentUserEmail = UserServiceFactory.getUserService()
                .getCurrentUser().getEmail().toLowerCase();
        final User currentUser = uDao.findUserByEmail(currentUserEmail);

        context.put("user", currentUser);
        context.put("permissions", getPermissionsMap(currentUser));

        final StringWriter writer = new StringWriter();
        t.merge(context, writer);

        resp.setContentType("application/javascript;charset=UTF-8");

        final PrintWriter pw = resp.getWriter();
        pw.println(writer.toString());
        pw.close();
    }

    /**
     * Retrieve a map of the paths and corresponding permissions for the current user
     *
     * @param currentUser
     * @return
     */
    private Map<String, Set<Permission>> getPermissionsMap(User currentUser) {
        List<UserAuthorization> authorizationList = userAuthorizationDAO.listByUser(currentUser
                .getKey().getId());
        Map<Long, UserRole> roleMap = new HashMap<Long, UserRole>();
        for (UserRole role : userRoleDAO.list(Constants.ALL_RESULTS)) {
            roleMap.put(role.getKey().getId(), role);
        }
        Map<String, Set<Permission>> permissions = new HashMap<String, Set<Permission>>();
        for (UserAuthorization auth : authorizationList) {
            UserRole role = roleMap.get(auth.getRoleId());
            if (role != null) {
                permissions.put(auth.getObjectPath(), role.getPermissions());
            }
        }
        return permissions;
    }
}

@@ -903,30 +903,8 @@
 ap;%0A
-import java.util.Set;%0A
 impo
@@ -3549,16 +3549,27 @@
 rieve a 
+javascript 
 map of t
@@ -3700,36 +3700,14 @@
 ate 
-Map%3C
 String
-, Set%3CPermission%3E%3E
  get
@@ -4090,36 +4090,21 @@
     
-Map%3C
 String
-, Set%3CPermission%3E%3E
+Builder
  per
@@ -4122,41 +4122,55 @@
 new 
-HashMap%3CString, Set%3CPermission%3E%3E(
+StringBuilder();%0A        permissions.append(%22%7B%22
 );%0A 
@@ -4346,20 +4346,30 @@
 issions.
-put(
+append(%22%5C%22%22 + 
 auth.get
@@ -4384,31 +4384,149 @@
 th()
-, role.getPermissions()
+ + %22%5C%22:%5B%22);%0A                for (Permission perm : role.getPermissions()) %7B%0A                    permissions.append(%22%5C%22%22 + perm.name() + %22%5C%22,%22
 );%0A 
@@ -4528,32 +4528,36 @@
 %22);%0A            
+ 
+   
 %7D%0A        %7D%0A    
@@ -4542,33 +4542,313 @@
       %7D%0A        
-%7D
+        permissions.append(%22%5D%22);%0A                permissions.append(%22,%22);%0A            %7D%0A        %7D%0A        if (permissions.lastIndexOf(%22,%22) == permissions.length() - 1) %7B%0A            permissions.deleteCharAt(permissions.lastIndexOf(%22,%22));%0A        %7D%0A        permissions.append(%22%7D%22);%0A
 %0A        return 
@@ -4858,16 +4858,27 @@
 missions
+.toString()
 ;%0A    %7D%0A

Return javascript map of permissions for currentUser