Search in sources :

Example 1 with ILayoutAttributeDescriptor

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

the class StylesheetUserPreferencesServiceImplTest method testThemeStylesheetUserPreferences.

/**
 * @throws Exception
 */
@Test
public void testThemeStylesheetUserPreferences() throws Exception {
    // Setup mocks
    final HttpServletRequest request = new MockHttpServletRequest();
    // initialize the session
    request.getSession();
    final IStylesheetDescriptorDao stylesheetDescriptorDao = mock(IStylesheetDescriptorDao.class);
    final IUserInstanceManager userInstanceManager = mock(IUserInstanceManager.class);
    final IStylesheetUserPreferencesDao stylesheetUserPreferencesDao = mock(IStylesheetUserPreferencesDao.class);
    final IFragmentDefinitionUtils fragmentUtils = mock(IFragmentDefinitionUtils.class);
    final IUserInstance userInstance = mock(IUserInstance.class);
    when(userInstanceManager.getUserInstance(request)).thenReturn(userInstance);
    final IPerson person = mock(IPerson.class);
    when(userInstance.getPerson()).thenReturn(person);
    final IUserPreferencesManager preferencesManager = mock(IUserPreferencesManager.class);
    when(userInstance.getPreferencesManager()).thenReturn(preferencesManager);
    final IUserProfile userProfile = mock(IUserProfile.class);
    when(preferencesManager.getUserProfile()).thenReturn(userProfile);
    final IUserLayoutManager userLayoutManager = mock(IUserLayoutManager.class);
    when(preferencesManager.getUserLayoutManager()).thenReturn(userLayoutManager);
    final IUserLayout userLayout = mock(IUserLayout.class);
    when(userLayoutManager.getUserLayout()).thenReturn(userLayout);
    when(userProfile.getThemeStylesheetId()).thenReturn(1);
    final IStylesheetDescriptor stylesheetDescriptor = mock(IStylesheetDescriptor.class);
    when(stylesheetDescriptorDao.getStylesheetDescriptor(1)).thenReturn(stylesheetDescriptor);
    final ILayoutAttributeDescriptor skinLayoutAttributeDescriptor = mock(ILayoutAttributeDescriptor.class);
    when(stylesheetDescriptor.getLayoutAttributeDescriptor("minimized")).thenReturn(skinLayoutAttributeDescriptor);
    when(skinLayoutAttributeDescriptor.getName()).thenReturn("minimized");
    when(skinLayoutAttributeDescriptor.getScope()).thenReturn(Scope.REQUEST);
    when(skinLayoutAttributeDescriptor.getTargetElementNames()).thenReturn(Collections.singleton("folder"));
    final IOutputPropertyDescriptor mediaOutputPropertyDescriptor = mock(IOutputPropertyDescriptor.class);
    when(stylesheetDescriptor.getOutputPropertyDescriptor("media")).thenReturn(mediaOutputPropertyDescriptor);
    when(mediaOutputPropertyDescriptor.getName()).thenReturn("media");
    when(mediaOutputPropertyDescriptor.getScope()).thenReturn(Scope.SESSION);
    final IStylesheetParameterDescriptor skinStylesheetParameterDescriptor = mock(IStylesheetParameterDescriptor.class);
    when(stylesheetDescriptor.getStylesheetParameterDescriptor("skin")).thenReturn(skinStylesheetParameterDescriptor);
    when(skinStylesheetParameterDescriptor.getName()).thenReturn("media");
    when(skinStylesheetParameterDescriptor.getScope()).thenReturn(Scope.PERSISTENT);
    final IStylesheetUserPreferences persistentStylesheetUserPreferences = mock(IStylesheetUserPreferences.class);
    when(stylesheetUserPreferencesDao.createStylesheetUserPreferences(stylesheetDescriptor, person, userProfile)).thenReturn(persistentStylesheetUserPreferences);
    when(stylesheetUserPreferencesDao.getStylesheetUserPreferences(stylesheetDescriptor, person, userProfile)).thenReturn(persistentStylesheetUserPreferences);
    when(persistentStylesheetUserPreferences.getStylesheetParameter("skin")).thenReturn(null).thenReturn("red");
    // Create and initialize service bean
    final StylesheetUserPreferencesServiceImpl stylesheetUserPreferencesService = new StylesheetUserPreferencesServiceImpl();
    stylesheetUserPreferencesService.setStylesheetDescriptorDao(stylesheetDescriptorDao);
    stylesheetUserPreferencesService.setUserInstanceManager(userInstanceManager);
    stylesheetUserPreferencesService.setStylesheetUserPreferencesDao(stylesheetUserPreferencesDao);
    stylesheetUserPreferencesService.setFragmentDefinitionUtils(fragmentUtils);
    // Run test
    String actual;
    actual = stylesheetUserPreferencesService.getLayoutAttribute(request, PreferencesScope.THEME, "u1l1n1", "minimized");
    assertNull(actual);
    actual = stylesheetUserPreferencesService.setLayoutAttribute(request, PreferencesScope.THEME, "u1l1n1", "minimized", "true");
    assertNull(actual);
    actual = stylesheetUserPreferencesService.getLayoutAttribute(request, PreferencesScope.THEME, "u1l1n1", "minimized");
    assertEquals("true", actual);
    actual = stylesheetUserPreferencesService.getStylesheetParameter(request, PreferencesScope.THEME, "skin");
    assertNull(actual);
    actual = stylesheetUserPreferencesService.setStylesheetParameter(request, PreferencesScope.THEME, "skin", "red");
    verify(persistentStylesheetUserPreferences).setStylesheetParameter("skin", "red");
    assertNull(actual);
    actual = stylesheetUserPreferencesService.getStylesheetParameter(request, PreferencesScope.THEME, "skin");
    assertEquals("red", actual);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) IStylesheetDescriptorDao(org.apereo.portal.layout.dao.IStylesheetDescriptorDao) ILayoutAttributeDescriptor(org.apereo.portal.layout.om.ILayoutAttributeDescriptor) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) IUserInstance(org.apereo.portal.user.IUserInstance) IPerson(org.apereo.portal.security.IPerson) IOutputPropertyDescriptor(org.apereo.portal.layout.om.IOutputPropertyDescriptor) IStylesheetParameterDescriptor(org.apereo.portal.layout.om.IStylesheetParameterDescriptor) IStylesheetUserPreferencesDao(org.apereo.portal.layout.dao.IStylesheetUserPreferencesDao) IUserProfile(org.apereo.portal.IUserProfile) IStylesheetDescriptor(org.apereo.portal.layout.om.IStylesheetDescriptor) IStylesheetUserPreferences(org.apereo.portal.layout.om.IStylesheetUserPreferences) IUserPreferencesManager(org.apereo.portal.IUserPreferencesManager) IUserInstanceManager(org.apereo.portal.user.IUserInstanceManager) IFragmentDefinitionUtils(org.apereo.portal.utils.IFragmentDefinitionUtils) Test(org.junit.Test)

Example 2 with ILayoutAttributeDescriptor

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

the class StylesheetAttributeSource method getAdditionalAttributes.

@Override
public final Iterator<Attribute> getAdditionalAttributes(HttpServletRequest request, HttpServletResponse response, StartElement event) {
    final IStylesheetDescriptor stylesheetDescriptor = this.getStylesheetDescriptor(request);
    final PreferencesScope stylesheetPreferencesScope = this.getStylesheetPreferencesScope(request);
    final Collection<Attribute> attributes = new LinkedList<Attribute>();
    for (final ILayoutAttributeDescriptor layoutAttributeDescriptor : stylesheetDescriptor.getLayoutAttributeDescriptors()) {
        final Set<String> targetElementNames = layoutAttributeDescriptor.getTargetElementNames();
        final QName eventName = event.getName();
        final String localEventName = eventName.getLocalPart();
        if (targetElementNames.contains(localEventName)) {
            final Attribute subscribeIdAttr = event.getAttributeByName(IUserLayoutManager.ID_ATTR_NAME);
            final String subscribeId = subscribeIdAttr.getValue();
            final String name = layoutAttributeDescriptor.getName();
            String value = this.stylesheetUserPreferencesService.getLayoutAttribute(request, stylesheetPreferencesScope, subscribeId, name);
            if (value == null) {
                value = layoutAttributeDescriptor.getDefaultValue();
            }
            if (value != null) {
                if (this.shouldDoSpelEvaluationForAttributeValue(value)) {
                    final ServletWebRequest webRequest = new ServletWebRequest(request, response);
                    value = this.doSpelEvaluationForAttributeValue(webRequest, value);
                }
                if (value != null) {
                    final Attribute attribute = xmlEventFactory.createAttribute(name, value);
                    attributes.add(attribute);
                }
            }
        }
    }
    return attributes.iterator();
}
Also used : Attribute(javax.xml.stream.events.Attribute) QName(javax.xml.namespace.QName) IStylesheetDescriptor(org.apereo.portal.layout.om.IStylesheetDescriptor) PreferencesScope(org.apereo.portal.layout.IStylesheetUserPreferencesService.PreferencesScope) ILayoutAttributeDescriptor(org.apereo.portal.layout.om.ILayoutAttributeDescriptor) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) LinkedList(java.util.LinkedList)

Example 3 with ILayoutAttributeDescriptor

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

the class StylesheetDescriptorImporterExporter method importData.

/* (non-Javadoc)
     * @see org.apereo.portal.io.xml.IDataImporterExporter#importData(java.lang.Object)
     */
@Transactional
@Override
public void importData(ExternalStylesheetDescriptor data) {
    final String stylesheetName = data.getName();
    final String uri = data.getUri();
    IStylesheetDescriptor stylesheetDescriptor = this.stylesheetDescriptorDao.getStylesheetDescriptorByName(stylesheetName);
    if (stylesheetDescriptor == null) {
        stylesheetDescriptor = this.stylesheetDescriptorDao.createStylesheetDescriptor(stylesheetName, uri);
    } else {
        stylesheetDescriptor.setStylesheetResource(uri);
    }
    stylesheetDescriptor.setUrlNodeSyntaxHelperName(data.getUrlSyntaxHelper());
    stylesheetDescriptor.setDescription(data.getDescription());
    final List<ExternalOutputPropertyDescriptor> extOutputProperties = data.getOutputProperties();
    final List<IOutputPropertyDescriptor> outputPropertyDescriptors = new ArrayList<IOutputPropertyDescriptor>(extOutputProperties.size());
    for (final ExternalOutputPropertyDescriptor extOutputProperty : extOutputProperties) {
        final String name = extOutputProperty.getName();
        final Scope scope = Scope.valueOf(extOutputProperty.getScope().name());
        final OutputPropertyDescriptorImpl outputPropertyDescriptor = new OutputPropertyDescriptorImpl(name, scope);
        outputPropertyDescriptor.setDefaultValue(extOutputProperty.getDefaultValue());
        outputPropertyDescriptor.setDescription(extOutputProperty.getDescription());
        outputPropertyDescriptors.add(outputPropertyDescriptor);
    }
    stylesheetDescriptor.setOutputPropertyDescriptors(outputPropertyDescriptors);
    final List<ExternalStylesheetParameterDescriptor> extStylesheetParameters = data.getStylesheetParameters();
    final List<IStylesheetParameterDescriptor> stylesheetParameterDescriptors = new ArrayList<IStylesheetParameterDescriptor>(extOutputProperties.size());
    for (final ExternalStylesheetParameterDescriptor extStylesheetParameter : extStylesheetParameters) {
        final String name = extStylesheetParameter.getName();
        final Scope scope = Scope.valueOf(extStylesheetParameter.getScope().name());
        final StylesheetParameterDescriptorImpl stylesheetParameterDescriptor = new StylesheetParameterDescriptorImpl(name, scope);
        stylesheetParameterDescriptor.setDefaultValue(extStylesheetParameter.getDefaultValue());
        stylesheetParameterDescriptor.setDescription(extStylesheetParameter.getDescription());
        stylesheetParameterDescriptors.add(stylesheetParameterDescriptor);
    }
    stylesheetDescriptor.setStylesheetParameterDescriptors(stylesheetParameterDescriptors);
    final List<ExternalLayoutAttributeDescriptor> extLayoutAttributes = data.getLayoutAttributes();
    final List<ILayoutAttributeDescriptor> layoutAttributeDescriptors = new ArrayList<ILayoutAttributeDescriptor>(extOutputProperties.size());
    for (final ExternalLayoutAttributeDescriptor extLayoutAttribute : extLayoutAttributes) {
        final String name = extLayoutAttribute.getName();
        final Scope scope = Scope.valueOf(extLayoutAttribute.getScope().name());
        final LayoutAttributeDescriptorImpl layoutAttributeDescriptor = new LayoutAttributeDescriptorImpl(name, scope);
        layoutAttributeDescriptor.setDefaultValue(extLayoutAttribute.getDefaultValue());
        layoutAttributeDescriptor.setDescription(extLayoutAttribute.getDescription());
        layoutAttributeDescriptor.setTargetElementNames(new LinkedHashSet<String>(extLayoutAttribute.getTargetElements()));
        layoutAttributeDescriptors.add(layoutAttributeDescriptor);
    }
    stylesheetDescriptor.setLayoutAttributeDescriptors(layoutAttributeDescriptors);
    this.stylesheetDescriptorDao.updateStylesheetDescriptor(stylesheetDescriptor);
}
Also used : LayoutAttributeDescriptorImpl(org.apereo.portal.layout.dao.jpa.LayoutAttributeDescriptorImpl) OutputPropertyDescriptorImpl(org.apereo.portal.layout.dao.jpa.OutputPropertyDescriptorImpl) ArrayList(java.util.ArrayList) StylesheetParameterDescriptorImpl(org.apereo.portal.layout.dao.jpa.StylesheetParameterDescriptorImpl) ILayoutAttributeDescriptor(org.apereo.portal.layout.om.ILayoutAttributeDescriptor) IOutputPropertyDescriptor(org.apereo.portal.layout.om.IOutputPropertyDescriptor) IStylesheetParameterDescriptor(org.apereo.portal.layout.om.IStylesheetParameterDescriptor) Scope(org.apereo.portal.layout.om.IStylesheetData.Scope) IStylesheetDescriptor(org.apereo.portal.layout.om.IStylesheetDescriptor) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with ILayoutAttributeDescriptor

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

the class StylesheetUserPreferencesServiceImpl method removeLayoutAttribute.

@Transactional
@Override
@SuppressWarnings("FallThrough")
public String removeLayoutAttribute(HttpServletRequest request, PreferencesScope prefScope, String nodeId, String name) {
    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 remove layout attribute {} for ID=\"{}\" but no such stylesheet parameter is defined in stylesheet descriptor {}. It will be ignored.", new Object[] { name, nodeId, stylesheetDescriptor.getName() });
        return null;
    }
    final Scope scope = this.getWriteScope(request, prefScope, stylesheetPreferencesKey, layoutAttributeDescriptor);
    switch(scope) {
        case PERSISTENT:
            {
                final IStylesheetUserPreferences stylesheetUserPreferences = this.getStylesheetUserPreferences(request, stylesheetPreferencesKey);
                if (stylesheetUserPreferences == null) {
                    break;
                }
                final String oldValue = stylesheetUserPreferences.removeLayoutAttribute(nodeId, name);
                if (oldValue != null) {
                    this.stylesheetUserPreferencesDao.storeStylesheetUserPreferences(stylesheetUserPreferences);
                    return oldValue;
                }
            }
        default:
            {
                final Map<String, String> nodeAttributes = this.getDataValue(request, stylesheetPreferencesKey, scope, LAYOUT_ATTRIBUTES_KEY, nodeId);
                if (nodeAttributes == null) {
                    break;
                }
                final String oldValue = nodeAttributes.remove(name);
                if (oldValue != null) {
                    return oldValue;
                }
            }
    }
    final IStylesheetUserPreferences distributedStylesheetUserPreferences = this.getDistributedStylesheetUserPreferences(request, prefScope);
    if (distributedStylesheetUserPreferences != null) {
        return distributedStylesheetUserPreferences.removeLayoutAttribute(nodeId, name);
    }
    return null;
}
Also used : Scope(org.apereo.portal.layout.om.IStylesheetData.Scope) IStylesheetDescriptor(org.apereo.portal.layout.om.IStylesheetDescriptor) IStylesheetUserPreferences(org.apereo.portal.layout.om.IStylesheetUserPreferences) ILayoutAttributeDescriptor(org.apereo.portal.layout.om.ILayoutAttributeDescriptor) 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)

Example 5 with ILayoutAttributeDescriptor

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

the class StylesheetUserPreferencesServiceImpl method populateLayoutAttributes.

@Override
public <P extends Populator<String, String>> P populateLayoutAttributes(HttpServletRequest request, PreferencesScope prefScope, String nodeId, P layoutAttributes) {
    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, Map<String, String>> sessionLayoutAttributes;
    final HttpSession session = request.getSession(false);
    if (session == null) {
        sessionLayoutAttributes = null;
    } else {
        sessionLayoutAttributes = getSessionLayoutAttributes(session, stylesheetPreferencesKey);
    }
    final Map<String, Map<String, String>> requestLayoutAttributes = getRequestLayoutAttributes(request, stylesheetPreferencesKey);
    final IStylesheetUserPreferences distributedStylesheetUserPreferences = this.getDistributedStylesheetUserPreferences(request, prefScope);
    if (distributedStylesheetUserPreferences != null) {
        distributedStylesheetUserPreferences.populateLayoutAttributes(nodeId, layoutAttributes);
    }
    // Try getting each layout attribute to populate the Map
    for (final ILayoutAttributeDescriptor layoutAttributeDescriptor : stylesheetDescriptor.getLayoutAttributeDescriptors()) {
        final String name = layoutAttributeDescriptor.getName();
        String value;
        final Scope scope = layoutAttributeDescriptor.getScope();
        switch(scope) {
            case PERSISTENT:
                {
                    if (stylesheetUserPreferences == null) {
                        value = null;
                        break;
                    }
                    value = stylesheetUserPreferences.getLayoutAttribute(nodeId, name);
                    break;
                }
            case SESSION:
                {
                    if (sessionLayoutAttributes == null) {
                        value = null;
                        break;
                    }
                    final Map<String, String> nodeAttributes = sessionLayoutAttributes.get(nodeId);
                    if (nodeAttributes == null) {
                        value = null;
                        break;
                    }
                    value = nodeAttributes.get(name);
                    break;
                }
            case REQUEST:
                {
                    if (requestLayoutAttributes == null) {
                        value = null;
                        break;
                    }
                    final Map<String, String> nodeAttributes = requestLayoutAttributes.get(nodeId);
                    if (nodeAttributes == null) {
                        value = null;
                        break;
                    }
                    value = nodeAttributes.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, layoutAttributeDescriptor.getDefaultValue())) {
            this.removeLayoutAttribute(request, prefScope, nodeId, name);
            continue;
        }
        layoutAttributes.put(name, value);
    }
    return layoutAttributes;
}
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) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

ILayoutAttributeDescriptor (org.apereo.portal.layout.om.ILayoutAttributeDescriptor)9 IStylesheetDescriptor (org.apereo.portal.layout.om.IStylesheetDescriptor)7 Scope (org.apereo.portal.layout.om.IStylesheetData.Scope)5 IStylesheetUserPreferences (org.apereo.portal.layout.om.IStylesheetUserPreferences)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 IOutputPropertyDescriptor (org.apereo.portal.layout.om.IOutputPropertyDescriptor)3 IStylesheetParameterDescriptor (org.apereo.portal.layout.om.IStylesheetParameterDescriptor)3 Transactional (org.springframework.transaction.annotation.Transactional)3 HttpSession (javax.servlet.http.HttpSession)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 QName (javax.xml.namespace.QName)1 Attribute (javax.xml.stream.events.Attribute)1 IUserPreferencesManager (org.apereo.portal.IUserPreferencesManager)1 IUserProfile (org.apereo.portal.IUserProfile)1 PreferencesScope (org.apereo.portal.layout.IStylesheetUserPreferencesService.PreferencesScope)1