Search in sources :

Example 21 with IStylesheetDescriptor

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

the class StylesheetUserPreferencesServiceImpl method removeStylesheetParameter.

@Transactional
@Override
public String removeStylesheetParameter(HttpServletRequest request, PreferencesScope prefScope, String name) {
    final StylesheetPreferencesKey stylesheetPreferencesKey = this.getStylesheetPreferencesKey(request, prefScope);
    final IStylesheetDescriptor stylesheetDescriptor = stylesheetPreferencesKey.stylesheetDescriptor;
    final IStylesheetParameterDescriptor stylesheetParameterDescriptor = stylesheetDescriptor.getStylesheetParameterDescriptor(name);
    if (stylesheetParameterDescriptor == null) {
        logger.warn("Attempted to remove stylesheet parameter {} but no such stylesheet parameter is defined in stylesheet descriptor {}. It will be ignored", new Object[] { name, stylesheetDescriptor.getName() });
        return null;
    }
    final Scope scope = this.getWriteScope(request, prefScope, stylesheetPreferencesKey, stylesheetParameterDescriptor);
    switch(scope) {
        case PERSISTENT:
            {
                final IStylesheetUserPreferences stylesheetUserPreferences = this.getStylesheetUserPreferences(request, stylesheetPreferencesKey);
                if (stylesheetUserPreferences == null) {
                    return null;
                }
                final String oldValue = stylesheetUserPreferences.removeStylesheetParameter(name);
                this.stylesheetUserPreferencesDao.storeStylesheetUserPreferences(stylesheetUserPreferences);
                return oldValue;
            }
        default:
            {
                return removeDataValue(request, stylesheetPreferencesKey, scope, STYLESHEET_PARAMETERS_KEY, name);
            }
    }
}
Also used : IStylesheetParameterDescriptor(org.apereo.portal.layout.om.IStylesheetParameterDescriptor) Scope(org.apereo.portal.layout.om.IStylesheetData.Scope) IStylesheetDescriptor(org.apereo.portal.layout.om.IStylesheetDescriptor) IStylesheetUserPreferences(org.apereo.portal.layout.om.IStylesheetUserPreferences) Transactional(org.springframework.transaction.annotation.Transactional)

Example 22 with IStylesheetDescriptor

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

the class RDBMDistributedLayoutStore method addStylesheetUserPreferencesAttributes.

private void addStylesheetUserPreferencesAttributes(IPerson person, IUserProfile profile, org.dom4j.Document layoutDoc, int stylesheetId, String attributeType) {
    final IStylesheetDescriptor structureStylesheetDescriptor = this.stylesheetDescriptorDao.getStylesheetDescriptor(stylesheetId);
    final IStylesheetUserPreferences ssup = this.stylesheetUserPreferencesDao.getStylesheetUserPreferences(structureStylesheetDescriptor, person, profile);
    if (ssup != null) {
        final Collection<String> allLayoutAttributeNodeIds = ssup.getAllLayoutAttributeNodeIds();
        for (final String nodeId : allLayoutAttributeNodeIds) {
            final MapPopulator<String, String> layoutAttributesPopulator = new MapPopulator<String, String>();
            ssup.populateLayoutAttributes(nodeId, layoutAttributesPopulator);
            final Map<String, String> layoutAttributes = layoutAttributesPopulator.getMap();
            final org.dom4j.Element element = layoutDoc.elementByID(nodeId);
            if (element == null) {
                logger.warn("No node with id '{}' found in layout for: {}. Stylesheet user preference layout attributes will be ignored: {}", nodeId, person.getUserName(), layoutAttributes);
                continue;
            }
            for (final Entry<String, String> attributeEntry : layoutAttributes.entrySet()) {
                final String name = attributeEntry.getKey();
                final String value = attributeEntry.getValue();
                logger.debug("Adding structure folder attribute:  name={}, value={}", name, value);
                final org.dom4j.Element structAttrElement = this.fac.createElement(attributeType + "-attribute");
                final org.dom4j.Element nameAttribute = structAttrElement.addElement("name");
                nameAttribute.setText(name);
                final org.dom4j.Element valueAttribute = structAttrElement.addElement("value");
                valueAttribute.setText(value);
                element.elements().add(0, structAttrElement);
            }
        }
    } else {
        logger.debug("no StylesheetUserPreferences found for {}, {}", person, profile);
    }
}
Also used : IStylesheetDescriptor(org.apereo.portal.layout.om.IStylesheetDescriptor) IStylesheetUserPreferences(org.apereo.portal.layout.om.IStylesheetUserPreferences) MapPopulator(org.apereo.portal.utils.MapPopulator)

Example 23 with IStylesheetDescriptor

use of org.apereo.portal.layout.om.IStylesheetDescriptor 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 24 with IStylesheetDescriptor

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

the class RDBMDistributedLayoutStore method loadDistributedStylesheetUserPreferences.

private IStylesheetUserPreferences loadDistributedStylesheetUserPreferences(IPerson person, IUserProfile profile, long stylesheetDescriptorId, Set<String> fragmentNames) {
    final boolean isFragmentOwner = this.isFragmentOwner(person);
    final Locale locale = profile.getLocaleManager().getLocales()[0];
    final IStylesheetDescriptor stylesheetDescriptor = this.stylesheetDescriptorDao.getStylesheetDescriptor(stylesheetDescriptorId);
    final IStylesheetUserPreferences stylesheetUserPreferences = this.stylesheetUserPreferencesDao.getStylesheetUserPreferences(stylesheetDescriptor, person, profile);
    final IStylesheetUserPreferences distributedStylesheetUserPreferences = new StylesheetUserPreferencesImpl();
    for (final String fragName : fragmentNames) {
        final FragmentDefinition fragmentDefinition = this.fragmentUtils.getFragmentDefinitionByName(fragName);
        //UserView may be missing if the fragment isn't defined correctly
        final UserView userView = this.fragmentUtils.getUserView(fragmentDefinition, locale);
        if (userView == null) {
            logger.warn("No UserView is present for fragment {} it will be skipped when loading distributed stylesheet user preferences", fragmentDefinition.getName());
            continue;
        }
        //IStylesheetUserPreferences only exist if something was actually set
        final IStylesheetUserPreferences fragmentStylesheetUserPreferences = this.stylesheetUserPreferencesDao.getStylesheetUserPreferences(stylesheetDescriptor, userView.getUserId(), userView.getProfileId());
        if (fragmentStylesheetUserPreferences == null) {
            continue;
        }
        //Get the info needed to DLMify node IDs
        final Element root = userView.getLayout().getDocumentElement();
        final String labelBase = root.getAttribute(Constants.ATT_ID);
        boolean modified = false;
        // Copy all of the fragment preferences into the distributed preferences
        final Collection<String> allLayoutAttributeNodeIds = fragmentStylesheetUserPreferences.getAllLayoutAttributeNodeIds();
        for (final String fragmentNodeId : allLayoutAttributeNodeIds) {
            final String userNodeId = (isFragmentOwner || fragmentNodeId.startsWith(Constants.FRAGMENT_ID_USER_PREFIX)) ? fragmentNodeId : labelBase + fragmentNodeId;
            final MapPopulator<String, String> layoutAttributesPopulator = new MapPopulator<String, String>();
            fragmentStylesheetUserPreferences.populateLayoutAttributes(fragmentNodeId, layoutAttributesPopulator);
            final Map<String, String> layoutAttributes = layoutAttributesPopulator.getMap();
            for (final Map.Entry<String, String> layoutAttributesEntry : layoutAttributes.entrySet()) {
                final String name = layoutAttributesEntry.getKey();
                final String value = layoutAttributesEntry.getValue();
                // Fragmentize the nodeId here
                distributedStylesheetUserPreferences.setLayoutAttribute(userNodeId, name, value);
                // are identical and removing layout attributes here would affect the fragment layout.
                if (stylesheetUserPreferences != null && !isFragmentOwner) {
                    final String userValue = stylesheetUserPreferences.getLayoutAttribute(userNodeId, name);
                    if (userValue != null && userValue.equals(value)) {
                        stylesheetUserPreferences.removeLayoutAttribute(userNodeId, name);
                        EditManager.removePreferenceDirective(person, userNodeId, name);
                        modified = true;
                    }
                }
            }
        }
        if (modified) {
            this.stylesheetUserPreferencesDao.storeStylesheetUserPreferences(stylesheetUserPreferences);
        }
    }
    return distributedStylesheetUserPreferences;
}
Also used : Locale(java.util.Locale) Element(org.w3c.dom.Element) IStylesheetDescriptor(org.apereo.portal.layout.om.IStylesheetDescriptor) IStylesheetUserPreferences(org.apereo.portal.layout.om.IStylesheetUserPreferences) StylesheetUserPreferencesImpl(org.apereo.portal.layout.StylesheetUserPreferencesImpl) Map(java.util.Map) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) MapPopulator(org.apereo.portal.utils.MapPopulator)

Example 25 with IStylesheetDescriptor

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

the class RDBMDistributedLayoutStore method loadStylesheetUserPreferencesAttributes.

private void loadStylesheetUserPreferencesAttributes(IPerson person, IUserProfile profile, org.dom4j.Element layout, final int structureStylesheetId, final String nodeType) {
    final IStylesheetDescriptor stylesheetDescriptor = this.stylesheetDescriptorDao.getStylesheetDescriptor(structureStylesheetId);
    final List<org.dom4j.Element> structureAttributes = layout.selectNodes("//" + nodeType + "-attribute");
    IStylesheetUserPreferences ssup = this.stylesheetUserPreferencesDao.getStylesheetUserPreferences(stylesheetDescriptor, person, profile);
    if (structureAttributes.isEmpty()) {
        if (ssup != null) {
            this.stylesheetUserPreferencesDao.deleteStylesheetUserPreferences(ssup);
        }
    } else {
        if (ssup == null) {
            ssup = this.stylesheetUserPreferencesDao.createStylesheetUserPreferences(stylesheetDescriptor, person, profile);
        }
        final Map<String, Map<String, String>> oldLayoutAttributes = new HashMap<String, Map<String, String>>();
        for (final String nodeId : ssup.getAllLayoutAttributeNodeIds()) {
            final MapPopulator<String, String> nodeAttributes = new MapPopulator<String, String>();
            ssup.populateLayoutAttributes(nodeId, nodeAttributes);
            oldLayoutAttributes.put(nodeId, nodeAttributes.getMap());
        }
        for (final org.dom4j.Element structureAttribute : structureAttributes) {
            final org.dom4j.Element layoutElement = structureAttribute.getParent();
            final String nodeId = layoutElement.valueOf("@ID");
            if (StringUtils.isEmpty(nodeId)) {
                logger.warn("@ID is empty for layout element, the attribute will be ignored: {}", structureAttribute.asXML());
            }
            final String name = structureAttribute.valueOf("name");
            if (StringUtils.isEmpty(nodeId)) {
                logger.warn("name is empty for layout element, the attribute will be ignored: {}", structureAttribute.asXML());
                continue;
            }
            final String value = structureAttribute.valueOf("value");
            if (StringUtils.isEmpty(nodeId)) {
                logger.warn("value is empty for layout element, the attribute will be ignored: {}", structureAttribute.asXML());
                continue;
            }
            //Remove from the old attrs set as we've updated it
            final Map<String, String> oldAttrs = oldLayoutAttributes.get(nodeId);
            if (oldAttrs != null) {
                oldAttrs.remove(name);
            }
            ssup.setLayoutAttribute(nodeId, name, value);
            // Remove the layout attribute element or DLM fails
            layoutElement.remove(structureAttribute);
        }
        //Purge orphaned entries
        for (final Entry<String, Map<String, String>> oldAttributeEntry : oldLayoutAttributes.entrySet()) {
            final String nodeId = oldAttributeEntry.getKey();
            for (final String name : oldAttributeEntry.getValue().keySet()) {
                ssup.removeLayoutAttribute(nodeId, name);
            }
        }
        this.stylesheetUserPreferencesDao.storeStylesheetUserPreferences(ssup);
    }
}
Also used : HashMap(java.util.HashMap) Element(org.w3c.dom.Element) IStylesheetDescriptor(org.apereo.portal.layout.om.IStylesheetDescriptor) IStylesheetUserPreferences(org.apereo.portal.layout.om.IStylesheetUserPreferences) Map(java.util.Map) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap) MapPopulator(org.apereo.portal.utils.MapPopulator)

Aggregations

IStylesheetDescriptor (org.apereo.portal.layout.om.IStylesheetDescriptor)31 IStylesheetUserPreferences (org.apereo.portal.layout.om.IStylesheetUserPreferences)15 Scope (org.apereo.portal.layout.om.IStylesheetData.Scope)11 IStylesheetParameterDescriptor (org.apereo.portal.layout.om.IStylesheetParameterDescriptor)10 Map (java.util.Map)7 ILayoutAttributeDescriptor (org.apereo.portal.layout.om.ILayoutAttributeDescriptor)7 Transactional (org.springframework.transaction.annotation.Transactional)6 ConcurrentMap (java.util.concurrent.ConcurrentMap)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 WindowState (javax.portlet.WindowState)4 HttpSession (javax.servlet.http.HttpSession)4 IUserProfile (org.apereo.portal.IUserProfile)4 IOutputPropertyDescriptor (org.apereo.portal.layout.om.IOutputPropertyDescriptor)4 IPerson (org.apereo.portal.security.IPerson)4 IUserInstance (org.apereo.portal.user.IUserInstance)4 MapPopulator (org.apereo.portal.utils.MapPopulator)4 IUserPreferencesManager (org.apereo.portal.IUserPreferencesManager)3 PreferencesScope (org.apereo.portal.layout.IStylesheetUserPreferencesService.PreferencesScope)3 IPortletEntity (org.apereo.portal.portlet.om.IPortletEntity)3