Search in sources :

Example 6 with LocaleManager

use of org.apereo.portal.i18n.LocaleManager in project uPortal by Jasig.

the class RDBMUserLayoutStore method getUserProfileByFname.

public UserProfile getUserProfileByFname(final IPerson person, final String profileFname) {
    Tuple<String, String> key = null;
    final Cache<Tuple<String, String>, UserProfile> profileCache = getProfileImportExportCache();
    if (profileCache != null) {
        key = new Tuple<String, String>(person.getUserName(), profileFname);
        final UserProfile profile = profileCache.getIfPresent(key);
        if (profile != null) {
            return profile;
        }
    }
    logger.debug("Getting profile {} for user {}", profileFname, person.getID());
    final int userId = person.getID();
    final UserProfile userProfile = jdbcOperations.execute(new ConnectionCallback<UserProfile>() {

        @Override
        public UserProfile doInConnection(Connection con) throws SQLException, DataAccessException {
            String query = "SELECT USER_ID, PROFILE_ID, PROFILE_NAME, DESCRIPTION, " + "LAYOUT_ID, STRUCTURE_SS_ID, THEME_SS_ID FROM UP_USER_PROFILE WHERE " + "USER_ID=? AND PROFILE_FNAME=?";
            PreparedStatement pstmt = con.prepareStatement(query);
            pstmt.setInt(1, userId);
            pstmt.setString(2, profileFname);
            try {
                logger.debug("getUserProfileByFname(): {} userId: {} profileFname: {}", query, userId, profileFname);
                ResultSet rs = pstmt.executeQuery();
                try {
                    if (rs.next()) {
                        int profileId = rs.getInt(2);
                        String profileName = rs.getString(3);
                        String profileDesc = rs.getString(4);
                        int layoutId = rs.getInt(5);
                        if (rs.wasNull()) {
                            layoutId = 0;
                        }
                        int structSsId = rs.getInt(6);
                        if (rs.wasNull()) {
                            // This is probably a data issue and probably an export operation;  defer to the system user...
                            if (!person.equals(getSystemUser())) {
                                structSsId = getSystemProfileByFname(profileFname).getStructureStylesheetId();
                            } else {
                                String msg = "The system user profile has no structure stylesheet Id.";
                                throw new IllegalStateException(msg);
                            }
                        }
                        int themeSsId = rs.getInt(7);
                        if (rs.wasNull()) {
                            // This is probably a data issue and probably an export operation;  defer to the system user...
                            if (!person.equals(getSystemUser())) {
                                themeSsId = getSystemProfileByFname(profileFname).getThemeStylesheetId();
                            } else {
                                String msg = "The system user profile has no theme stylesheet Id.";
                                throw new IllegalStateException(msg);
                            }
                        }
                        UserProfile userProfile = new UserProfile(profileId, profileFname, profileName, profileDesc, layoutId, structSsId, themeSsId);
                        final Locale[] userLocales = localeStore.getUserLocales(person);
                        userProfile.setLocaleManager(new LocaleManager(person, userLocales));
                        return userProfile;
                    }
                    /* Try to copy the template profile. */
                    logger.debug("Copying template profile {} to user {}", profileFname, person.getID());
                    rs.close();
                    pstmt.close();
                    pstmt = con.prepareStatement("SELECT USER_DFLT_USR_ID FROM UP_USER WHERE USER_ID=?");
                    pstmt.setInt(1, person.getID());
                    rs = pstmt.executeQuery();
                    if (rs.next()) {
                        int defaultProfileUser = rs.getInt(1);
                        if (rs.wasNull()) {
                            throw new RuntimeException("Need to clone the '" + profileFname + "' profile from template user for " + person + " but they have no template user");
                        }
                        IPerson defaultProfilePerson = new PersonImpl();
                        defaultProfilePerson.setID(defaultProfileUser);
                        if (defaultProfilePerson.getID() != person.getID()) {
                            UserProfile templateProfile = getUserProfileByFname(defaultProfilePerson, profileFname);
                            if (templateProfile != null) {
                                UserProfile newUserProfile = new UserProfile(templateProfile);
                                final Locale[] userLocales = localeStore.getUserLocales(person);
                                newUserProfile.setLayoutId(0);
                                newUserProfile = addUserProfile(person, newUserProfile);
                                newUserProfile.setLocaleManager(new LocaleManager(person, userLocales));
                                return newUserProfile;
                            }
                        }
                    }
                    throw new RuntimeException("Unable to find User Profile for userId " + userId + " and profile " + profileFname);
                } finally {
                    rs.close();
                }
            } finally {
                pstmt.close();
            }
        }
    });
    if (profileCache != null && key != null) {
        profileCache.put(key, userProfile);
    }
    return userProfile;
}
Also used : IUserProfile(org.apereo.portal.IUserProfile) UserProfile(org.apereo.portal.UserProfile) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) IJoinQueryString(org.apereo.portal.jdbc.IJoinQueryString) IPerson(org.apereo.portal.security.IPerson) PersonImpl(org.apereo.portal.security.provider.PersonImpl) ResultSet(java.sql.ResultSet) LocaleManager(org.apereo.portal.i18n.LocaleManager) Tuple(org.apereo.portal.utils.Tuple) DataAccessException(org.springframework.dao.DataAccessException)

Example 7 with LocaleManager

use of org.apereo.portal.i18n.LocaleManager in project uPortal by Jasig.

the class AbstractTenantOperationsListener method getCurrentUserLocale.

/*
     * Implementation
     */
private Locale getCurrentUserLocale() {
    final HttpServletRequest req = this.portalRequestUtils.getCurrentPortalRequest();
    final IPerson person = personManager.getPerson(req);
    final Locale[] userLocales = localeStore.getUserLocales(person);
    final LocaleManager localeManager = new LocaleManager(person, userLocales);
    final Locale locale = localeManager.getLocales()[0];
    return locale;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Locale(java.util.Locale) IPerson(org.apereo.portal.security.IPerson) LocaleManager(org.apereo.portal.i18n.LocaleManager)

Example 8 with LocaleManager

use of org.apereo.portal.i18n.LocaleManager in project uPortal by Jasig.

the class LocaleTransformerConfigurationSource method getCacheKey.

/* (non-Javadoc)
     * @see org.apereo.portal.rendering.xslt.TransformerConfigurationSource#getCacheKey(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
@Override
public CacheKey getCacheKey(HttpServletRequest request, HttpServletResponse response) {
    final LocaleManager localeManager = this.getLocaleManager(request);
    final Locale[] locales = localeManager.getLocales();
    if (locales != null && locales.length > 0 && locales[0] != null) {
        final String locale = locales[0].toString();
        final String xslLocale = locale.replace('_', '-');
        return CacheKey.build(this.getClass().getName(), xslLocale);
    }
    return null;
}
Also used : Locale(java.util.Locale) LocaleManager(org.apereo.portal.i18n.LocaleManager)

Example 9 with LocaleManager

use of org.apereo.portal.i18n.LocaleManager in project uPortal by Jasig.

the class ChannelListController method getUserLocale.

/*
     * Implementation
     */
private Locale getUserLocale(IPerson user) {
    // get user locale
    Locale[] locales = localeStore.getUserLocales(user);
    LocaleManager localeManager = new LocaleManager(user, locales);
    Locale rslt = localeManager.getLocales()[0];
    return rslt;
}
Also used : Locale(java.util.Locale) LocaleManager(org.apereo.portal.i18n.LocaleManager)

Example 10 with LocaleManager

use of org.apereo.portal.i18n.LocaleManager in project uPortal by Jasig.

the class UserLocaleHelper method getCurrentUserLocale.

/**
     * Return the current user's locale.
     *
     * @param request
     * @return
     */
public Locale getCurrentUserLocale(PortletRequest request) {
    final HttpServletRequest originalPortalRequest = this.portalRequestUtils.getPortletHttpRequest(request);
    IUserInstance ui = userInstanceManager.getUserInstance(originalPortalRequest);
    IUserPreferencesManager upm = ui.getPreferencesManager();
    final IUserProfile userProfile = upm.getUserProfile();
    LocaleManager localeManager = userProfile.getLocaleManager();
    // first check the session locales
    Locale[] sessionLocales = localeManager.getSessionLocales();
    if (sessionLocales != null && sessionLocales.length > 0) {
        return sessionLocales[0];
    }
    // if no session locales were found, check the user locales
    Locale[] userLocales = localeManager.getUserLocales();
    if (userLocales != null && userLocales.length > 0) {
        return userLocales[0];
    }
    // just return null
    return null;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) IUserInstance(org.apereo.portal.user.IUserInstance) Locale(java.util.Locale) IUserProfile(org.apereo.portal.IUserProfile) IUserPreferencesManager(org.apereo.portal.IUserPreferencesManager) LocaleManager(org.apereo.portal.i18n.LocaleManager)

Aggregations

LocaleManager (org.apereo.portal.i18n.LocaleManager)14 Locale (java.util.Locale)12 IUserProfile (org.apereo.portal.IUserProfile)5 IPerson (org.apereo.portal.security.IPerson)5 IUserInstance (org.apereo.portal.user.IUserInstance)4 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 IUserPreferencesManager (org.apereo.portal.IUserPreferencesManager)2 PortalException (org.apereo.portal.PortalException)2 IJoinQueryString (org.apereo.portal.jdbc.IJoinQueryString)2 Tuple (org.apereo.portal.utils.Tuple)2 DataAccessException (org.springframework.dao.DataAccessException)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 Statement (java.sql.Statement)1 HashMap (java.util.HashMap)1 HttpSession (javax.servlet.http.HttpSession)1