// Path: app/models/User.java
// @Entity
// @Table(name = "MPO_USER")
// public class User extends AbstractModel implements RoleHolder
// {
// 
//     @Column(nullable = false, length = 40, unique = true)
//     public String userName;
// 
//     @Column(nullable = false, length = 100)
//     public String displayName;
// 
//     @Column(name = "passwd", nullable = false, length = 64)
//     public String password;
// 
//     @Column(nullable = true, length = 2500)
//     public String avatarUrl;
// 
//     @Column(nullable = false)
//     public Boolean accountActive;
// 
//     @Column(nullable = true, length = 36)
//     public String confirmationCode;
// 
//     @ManyToMany(cascade = {CascadeType.DETACH, CascadeType.REFRESH})
//     public List<UserRole> roles;
// 
//     @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
//     public List<Vote> votes;
// 
//     @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
//     public List<Rate> rates;
// 
//     public static final Finder<Long, User> FIND = new Finder<Long, User>(Long.class,
//             User.class);
// 
//     public static int count() {
//         return FIND.findRowCount();
//     }
// 
//     public static User findByUserName(String userName) {
//         return FIND.where()
//                 .eq("userName", userName)
//                 .findUnique();
//     }
// 
//     public static User authenticate(String userName,
//                                     String password) {
//         return FIND.where()
//                 .eq("userName", userName)
//                 .eq("password", password)
//                 .findUnique();
//     }
// 
//     @Override
//     public List<? extends Role> getRoles()
//     {
//         return roles;
//     }
// 
//     @Override
//     public List<? extends Permission> getPermissions()
//     {
//         // for now, let's go with role-based security.  I don't think we need any fine-grained control.
//         return Collections.emptyList();
//     }
// }
// 
// Path: app/utils/StringUtils.java
// public class StringUtils
// {
//     /**
//      * Check if a string has non-empty content.
//      *
//      * @param s the string
//      * @return true if the string is null, empty or comprised only of empty characters spaces, tabs, \n, etc),
//      * otherwise false
//      */
//     public static boolean isEmpty(String s)
//     {
//         return s == null || s.trim().length() == 0;
//     }
//     
//     public static String summarize(String s)
//     {
//         String result = "";
//         if (!isEmpty(s))
//         {
//             result = s.length() <= 100 ? s : s.substring(0, 100) + "...";
//         }
//         return result;
//     }
// }


import com.avaje.ebean.Ebean;
import models.User;
import play.mvc.Action;
import play.mvc.Http;
import play.mvc.Result;
import utils.StringUtils;
/*
 * Copyright 2012 The Play! Framework
 *
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package actions;


/**
 * @author Steve Chaloner (steve@objectify.be)
 */
public class CurrentUser extends Action.Simple
{
    @Override
    public Result call(Http.Context ctx) throws Throwable
    {
        accessUser(ctx);
        return delegate.call(ctx);
    }

    private static User accessUser(Http.Context ctx)
    {
        User user = (User)ctx.args.get("user");
        if (user == null)
        {
            String userName = ctx.session().get("userName");
            if (!StringUtils.isEmpty(userName))
            {
                user = User.findByUserName(userName);
                if (user != null)
                {
                    ctx.args.put("user",
                                 user);
                }
            }
        }

        return user;
    }

// Path: app/models/User.java
// @Entity
// @Table(name = "MPO_USER")
// public class User extends AbstractModel implements RoleHolder
// {
// 
//     @Column(nullable = false, length = 40, unique = true)
//     public String userName;
// 
//     @Column(nullable = false, length = 100)
//     public String displayName;
// 
//     @Column(name = "passwd", nullable = false, length = 64)
//     public String password;
// 
//     @Column(nullable = true, length = 2500)
//     public String avatarUrl;
// 
//     @Column(nullable = false)
//     public Boolean accountActive;
// 
//     @Column(nullable = true, length = 36)
//     public String confirmationCode;
// 
//     @ManyToMany(cascade = {CascadeType.DETACH, CascadeType.REFRESH})
//     public List<UserRole> roles;
// 
//     @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
//     public List<Vote> votes;
// 
//     @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
//     public List<Rate> rates;
// 
//     public static final Finder<Long, User> FIND = new Finder<Long, User>(Long.class,
//             User.class);
// 
//     public static int count() {
//         return FIND.findRowCount();
//     }
// 
//     public static User findByUserName(String userName) {
//         return FIND.where()
//                 .eq("userName", userName)
//                 .findUnique();
//     }
// 
//     public static User authenticate(String userName,
//                                     String password) {
//         return FIND.where()
//                 .eq("userName", userName)
//                 .eq("password", password)
//                 .findUnique();
//     }
// 
//     @Override
//     public List<? extends Role> getRoles()
//     {
//         return roles;
//     }
// 
//     @Override
//     public List<? extends Permission> getPermissions()
//     {
//         // for now, let's go with role-based security.  I don't think we need any fine-grained control.
//         return Collections.emptyList();
//     }
// }
// 
// Path: app/utils/StringUtils.java
// public class StringUtils
// {
//     /**
//      * Check if a string has non-empty content.
//      *
//      * @param s the string
//      * @return true if the string is null, empty or comprised only of empty characters spaces, tabs, \n, etc),
//      * otherwise false
//      */
//     public static boolean isEmpty(String s)
//     {
//         return s == null || s.trim().length() == 0;
//     }
//     
//     public static String summarize(String s)
//     {
//         String result = "";
//         if (!isEmpty(s))
//         {
//             result = s.length() <= 100 ? s : s.substring(0, 100) + "...";
//         }
//         return result;
//     }
// }


// Path: app/actions/CurrentUser.java
import com.avaje.ebean.Ebean;
import models.User;
import play.mvc.Action;
import play.mvc.Http;
import play.mvc.Result;
import utils.StringUtils;

/*
 * Copyright 2012 The Play! Framework
 *
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package actions;


/**
 * @author Steve Chaloner (steve@objectify.be)
 */
public class CurrentUser extends Action.Simple
{
    @Override
    public Result call(Http.Context ctx) throws Throwable
    {
        accessUser(ctx);
        return delegate.call(ctx);
    }

    private static User accessUser(Http.Context ctx)
    {
        User user = (User)ctx.args.get("user");
        if (user == null)
        {
            String userName = ctx.session().get("userName");
            if (!StringUtils.isEmpty(userName))
            {
                user = User.findByUserName(userName);
                if (user != null)
                {
                    ctx.args.put("user",
                                 user);
                }
            }
        }

        return user;
    }

    public static User currentUser()