Search in sources :

Example 11 with Scope

use of org.apereo.portal.layout.om.IStylesheetData.Scope in project uPortal by Jasig.

the class StylesheetUserPreferencesServiceImpl method populateStylesheetParameters.

@Override
public <P extends Populator<String, String>> P populateStylesheetParameters(HttpServletRequest request, PreferencesScope prefScope, P stylesheetParameters) {
    final StylesheetPreferencesKey stylesheetPreferencesKey = this.getStylesheetPreferencesKey(request, prefScope);
    final IStylesheetDescriptor stylesheetDescriptor = stylesheetPreferencesKey.stylesheetDescriptor;
    // Get the scoped sources once
    final IStylesheetUserPreferences stylesheetUserPreferences = this.getStylesheetUserPreferences(request, stylesheetPreferencesKey);
    final Map<String, String> sessionStylesheetParameters;
    final HttpSession session = request.getSession(false);
    if (session == null) {
        sessionStylesheetParameters = null;
    } else {
        sessionStylesheetParameters = PortalWebUtils.getMapSessionAttribute(session, STYLESHEET_PARAMETERS_KEY + stylesheetPreferencesKey.toString(), false);
    }
    final Map<String, String> requestStylesheetParameters = PortalWebUtils.getMapRequestAttribute(request, STYLESHEET_PARAMETERS_KEY + stylesheetPreferencesKey.toString(), false);
    // Try getting each stylesheet parameter to populate the Map
    for (final IStylesheetParameterDescriptor stylesheetParameterDescriptor : stylesheetDescriptor.getStylesheetParameterDescriptors()) {
        final String name = stylesheetParameterDescriptor.getName();
        final String value;
        final Scope scope = stylesheetParameterDescriptor.getScope();
        switch(scope) {
            case PERSISTENT:
                {
                    if (stylesheetUserPreferences == null) {
                        value = null;
                        break;
                    }
                    value = stylesheetUserPreferences.getStylesheetParameter(name);
                    break;
                }
            case SESSION:
                {
                    if (sessionStylesheetParameters == null) {
                        value = null;
                        break;
                    }
                    value = sessionStylesheetParameters.get(name);
                    break;
                }
            case REQUEST:
                {
                    if (requestStylesheetParameters == null) {
                        value = null;
                        break;
                    }
                    value = requestStylesheetParameters.get(name);
                    break;
                }
            default:
                {
                    value = null;
                    break;
                }
        }
        // Don't add unset properties
        if (value == null) {
            continue;
        }
        // If the value is equal to the default value remove the property and return null
        if (this.compareValues(value, stylesheetParameterDescriptor.getDefaultValue())) {
            this.removeStylesheetParameter(request, prefScope, name);
            continue;
        }
        stylesheetParameters.put(name, value);
    }
    return stylesheetParameters;
}
Also used : IStylesheetParameterDescriptor(org.apereo.portal.layout.om.IStylesheetParameterDescriptor) Scope(org.apereo.portal.layout.om.IStylesheetData.Scope) HttpSession(javax.servlet.http.HttpSession) IStylesheetDescriptor(org.apereo.portal.layout.om.IStylesheetDescriptor) IStylesheetUserPreferences(org.apereo.portal.layout.om.IStylesheetUserPreferences)

Example 12 with Scope

use of org.apereo.portal.layout.om.IStylesheetData.Scope in project uPortal by Jasig.

the class StylesheetUserPreferencesServiceImpl method setLayoutAttribute.

@Transactional
@Override
public String setLayoutAttribute(HttpServletRequest request, PreferencesScope prefScope, String nodeId, String name, String value) {
    final StylesheetPreferencesKey stylesheetPreferencesKey = this.getStylesheetPreferencesKey(request, prefScope);
    final IStylesheetDescriptor stylesheetDescriptor = stylesheetPreferencesKey.stylesheetDescriptor;
    final ILayoutAttributeDescriptor layoutAttributeDescriptor = stylesheetDescriptor.getLayoutAttributeDescriptor(name);
    if (layoutAttributeDescriptor == null) {
        logger.warn("Attempted to set layout attribute {}={} on node with ID=\"{}\" but no such stylesheet parameter is defined in stylesheet descriptor {}. It will be ignored.", new Object[] { name, value, nodeId, stylesheetDescriptor.getName() });
        return null;
    }
    if (this.compareValues(value, layoutAttributeDescriptor.getDefaultValue())) {
        // Value matches the default value, remove the attribute
        return this.removeLayoutAttribute(request, prefScope, nodeId, name);
    }
    final IStylesheetUserPreferences distributedStylesheetUserPreferences = this.getDistributedStylesheetUserPreferences(request, prefScope);
    if (distributedStylesheetUserPreferences != null) {
        final String defaultValue = distributedStylesheetUserPreferences.getLayoutAttribute(nodeId, name);
        if (this.compareValues(value, defaultValue)) {
            // Value matches the DLM preferences value, remove the value
            return this.removeLayoutAttribute(request, prefScope, nodeId, name);
        }
    }
    final Scope scope = this.getWriteScope(request, prefScope, stylesheetPreferencesKey, layoutAttributeDescriptor);
    switch(scope) {
        case PERSISTENT:
            {
                IStylesheetUserPreferences stylesheetUserPreferences = this.getStylesheetUserPreferences(request, stylesheetPreferencesKey);
                if (stylesheetUserPreferences == null) {
                    stylesheetUserPreferences = this.stylesheetUserPreferencesDao.createStylesheetUserPreferences(stylesheetDescriptor, stylesheetPreferencesKey.person, stylesheetPreferencesKey.userProfile);
                    this.clearStylesheetUserPreferencesCache(request, stylesheetPreferencesKey);
                }
                final String oldValue = stylesheetUserPreferences.setLayoutAttribute(nodeId, name, value);
                this.stylesheetUserPreferencesDao.storeStylesheetUserPreferences(stylesheetUserPreferences);
                return oldValue;
            }
        default:
            {
                // Determine the mutex to use for accessing the nodeAttributes map
                final Object mutex;
                switch(scope) {
                    case REQUEST:
                        {
                            mutex = PortalWebUtils.getRequestAttributeMutex(request);
                            break;
                        }
                    case SESSION:
                        {
                            final HttpSession session = request.getSession();
                            mutex = WebUtils.getSessionMutex(session);
                            break;
                        }
                    default:
                        {
                            mutex = new Object();
                            break;
                        }
                }
                // Get/Create the nodeAttributes map
                Map<String, String> nodeAttributes;
                synchronized (mutex) {
                    nodeAttributes = this.getDataValue(request, stylesheetPreferencesKey, scope, LAYOUT_ATTRIBUTES_KEY, nodeId);
                    if (nodeAttributes == null) {
                        nodeAttributes = new ConcurrentHashMap<>();
                        this.putDataValue(request, stylesheetPreferencesKey, scope, LAYOUT_ATTRIBUTES_KEY, nodeId, nodeAttributes);
                    }
                }
                return nodeAttributes.put(name, value);
            }
    }
}
Also used : Scope(org.apereo.portal.layout.om.IStylesheetData.Scope) HttpSession(javax.servlet.http.HttpSession) IStylesheetDescriptor(org.apereo.portal.layout.om.IStylesheetDescriptor) IStylesheetUserPreferences(org.apereo.portal.layout.om.IStylesheetUserPreferences) ILayoutAttributeDescriptor(org.apereo.portal.layout.om.ILayoutAttributeDescriptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Scope (org.apereo.portal.layout.om.IStylesheetData.Scope)12 IStylesheetDescriptor (org.apereo.portal.layout.om.IStylesheetDescriptor)11 IStylesheetUserPreferences (org.apereo.portal.layout.om.IStylesheetUserPreferences)10 Transactional (org.springframework.transaction.annotation.Transactional)6 ILayoutAttributeDescriptor (org.apereo.portal.layout.om.ILayoutAttributeDescriptor)5 IStylesheetParameterDescriptor (org.apereo.portal.layout.om.IStylesheetParameterDescriptor)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 Map (java.util.Map)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 HttpSession (javax.servlet.http.HttpSession)4 IOutputPropertyDescriptor (org.apereo.portal.layout.om.IOutputPropertyDescriptor)3 ArrayList (java.util.ArrayList)1 LayoutAttributeDescriptorImpl (org.apereo.portal.layout.dao.jpa.LayoutAttributeDescriptorImpl)1 OutputPropertyDescriptorImpl (org.apereo.portal.layout.dao.jpa.OutputPropertyDescriptorImpl)1 StylesheetParameterDescriptorImpl (org.apereo.portal.layout.dao.jpa.StylesheetParameterDescriptorImpl)1