/**
 * Copyright 2015 LinkedIn Corp. All rights reserved.
 *
 * Licensed 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.
 */
package controllers.api.v1;

import com.fasterxml.jackson.databind.node.ObjectNode;
import dao.UserDAO;
import java.util.List;
import models.CompanyUser;
import models.Group;
import models.UserEntity;
import org.apache.commons.lang3.StringUtils;
import play.cache.Cache;
import play.libs.Json;
import play.mvc.Controller;
import play.mvc.Result;
import java.util.Map;


public class User extends Controller
{
    private static final String CACHE_INTERNAL_USERS = "internal.users.cache";
    private static final String CACHE_INTERNAL_GROUPS = "internal.groups.cache";
    private static final String CACHE_INTERNAL_ENTITIES = "internal.entities.cache";

    public static Result getLoggedInUser()
    {
        ObjectNode result = Json.newObject();

        result.put("status", "ok");
        String username = session("user");
        result.set("user", Json.toJson(UserDAO.getCurrentUser(username)));
        return ok(result);
    }


    public static Result updateSettings()
    {
        Map<String, String[]> params = request().body().asFormUrlEncoded();
        ObjectNode result = Json.newObject();

        String username = session("user");
        if (StringUtils.isNotBlank(username))
        {
            String message = UserDAO.updateUserSettings(params, username);
            if (StringUtils.isBlank(message))
            {
                result.put("status", "success");
            }
            else
            {
                result.put("status", "failed");
                result.put("message", message);
            }
        }
        else
        {
            result.put("status", "failed");
            result.put("message", "User is not authenticated");
        }
        return ok(result);
    }

    public static Result getAllCompanyUsers() {
        List<CompanyUser> users = (List<CompanyUser>) Cache.get(CACHE_INTERNAL_USERS);
        if (users == null || users.size() == 0) {
            users = UserDAO.getAllCompanyUsers();
            Cache.set(CACHE_INTERNAL_USERS, users, 24 * 3600); // cache for 24 hours
        }
        ObjectNode result = Json.newObject();
        result.put("status", "ok");
        result.set("employees", Json.toJson(users));
        return ok(result);
    }

    public static Result getAllGroups() {
        List<Group> groups = (List<Group>) Cache.get(CACHE_INTERNAL_GROUPS);
        if (groups == null || groups.size() == 0) {
            groups = UserDAO.getAllGroups();
            Cache.set(CACHE_INTERNAL_GROUPS, groups, 24 * 3600); // cache for 24 hours
        }
        ObjectNode result = Json.newObject();
        result.put("status", "ok");
        result.set("groups", Json.toJson(groups));
        return ok(result);
    }

    public static Result getAllUserEntities() {
        List<UserEntity> entities = (List<UserEntity>) Cache.get(CACHE_INTERNAL_ENTITIES);
        if (entities == null || entities.size() == 0) {
            entities = UserDAO.getAllUserEntities();
            Cache.set(CACHE_INTERNAL_ENTITIES, entities, 24 * 3600); // cache for 24 hours
        }
        ObjectNode result = Json.newObject();
        result.put("status", "ok");
        result.set("userEntities", Json.toJson(entities));
        return ok(result);
    }
}

@@ -1247,32 +1247,132 @@
 n.newObject();%0A%0A
+        String username = session(%22user%22);%0A        if (StringUtils.isBlank(username))%0A        %7B%0A    
         result.p
@@ -1377,34 +1377,38 @@
 .put(%22status%22, %22
-ok
+failed
 %22);%0A        Stri
@@ -1407,42 +1407,362 @@
     
-String username
+    result.put(%22message%22, %22no user in session%22);%0A            return ok(result);%0A        %7D%0A%0A        models.User user
  = 
+U
 se
-ssion(%22user%22);
+rDAO.getCurrentUser(username);%0A        if (StringUtils.isBlank(user.userName))%0A        %7B%0A            result.put(%22status%22, %22failed%22);%0A            result.put(%22message%22, %22can't find user info%22);%0A            return ok(result);%0A        %7D%0A
 %0A   
@@ -1801,41 +1801,49 @@
 son(
-UserDAO.getCurrentUser(username))
+user));%0A        result.put(%22status%22, %22ok%22
 );%0A 
@@ -1872,16 +1872,15 @@
 t);%0A
+
     %7D%0A%0A
-%0A
     

Add check for user not in session, at get current user API