Package org.internna.iwebmvc.core.context

Source Code of org.internna.iwebmvc.core.context.ContextHolder

/*
* Copyright 2002-2007 the original author or authors.
*
* 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 org.internna.iwebmvc.core.context;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.internna.iwebmvc.core.dao.DAO;
import org.internna.iwebmvc.core.dao.SecurityDAO;
import org.internna.iwebmvc.model.core.Locale;
import org.internna.iwebmvc.model.security.RoleImpl;
import org.internna.iwebmvc.model.security.UserImpl;
import org.internna.iwebmvc.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;

/**
* Holds a reference to some useful beans.
*
* @author Jose Noheda
* @since 1.0
*/
public class ContextHolder {

    protected static final Log log = LogFactory.getLog(ContextHolder.class);

    protected String googleAPIKey;
    protected UserImpl user;
    protected List<Locale> locales;
    @Autowired @Qualifier("baseDAO") protected DAO dao;
    @Autowired protected SecurityDAO securityDAO;

    @Required
    public void setUser(UserImpl user) {
        this.user = user;
    }

    public void setGoogleAPIKey(String key) {
        this.googleAPIKey = key;
    }

    public String getGoogleAPIKey() {
        return googleAPIKey;
    }

    @Resource
    public void setApplicationLocales(Map<String, String> applicationLocales) {
        Assert.notEmpty(applicationLocales);
        Map<String, Object> parameters = new HashMap<String, Object>(1);
        for (String locale : applicationLocales.keySet()) {
            parameters.put("locale", locale);
            List<Locale> l = dao.findByNamedQuery(Locale.FIND_BY_LOCALE, 0, 1, parameters);
            if ((l == null) || (l.size() == 0)) {
                Locale newLocale = getLocale(locale, applicationLocales.get(locale));
                dao.create(newLocale);
            }
        }
    }

    @Resource
    public void setApplicationAuthorities(List<String> applicationAuthorities) {
        Assert.notEmpty(applicationAuthorities);
        for (String authority : applicationAuthorities) securityDAO.createAuthority(authority);
    }

    protected void init() throws Exception {
        UserImpl root = null;
        try {
            root = securityDAO.findUser(user.getUsername());
        } catch (Exception ex) {
            if (log.isDebugEnabled()) log.debug("Root user [" + user.getUsername() + "] could not be found: " + ex.getMessage());
        }
        if (root == null) {
            List<RoleImpl> roles = securityDAO.findAuthorities();
            if (log.isDebugEnabled()) log.debug("Creating root user [" + user.getUsername() + "] with roles: " + roles);
            user.setRoles(new HashSet<RoleImpl>(roles));
            securityDAO.createUser(user);
        }
    }

    protected Locale getLocale(String locale, String description) {
        return new Locale(locale, description);
    }

    public List<Locale> getAvailableLocales() {
        return locales == null ? dao.find(Locale.class, 0, 50) : locales;
    }

    public List<RoleImpl> getAuthorities() {
        return securityDAO.findAuthorities();
    }

}
TOP

Related Classes of org.internna.iwebmvc.core.context.ContextHolder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.