Search in sources :

Example 1 with MapPopulator

use of org.apereo.portal.utils.MapPopulator in project uPortal by Jasig.

the class JpaStylesheetDescriptorDaoTest method testStylesheetUserPreferencesDao.

@Test
public void testStylesheetUserPreferencesDao() throws Exception {
    final long ssdId = this.execute(new Callable<Long>() {

        @Override
        public Long call() throws Exception {
            final IStylesheetDescriptor stylesheetDescriptor = stylesheetDescriptorDao.createStylesheetDescriptor("columns", "classpath:/layout/struct/columns.xsl");
            final long id = stylesheetDescriptor.getId();
            assertNotSame(-1, id);
            return id;
        }
    });
    final IPerson person = mock(IPerson.class);
    when(person.getID()).thenReturn(1);
    final IUserProfile userProfile = mock(IUserProfile.class);
    when(userProfile.getProfileId()).thenReturn(1);
    final long supId = this.execute(new Callable<Long>() {

        @Override
        public Long call() throws Exception {
            final IStylesheetDescriptor stylesheetDescriptor = stylesheetDescriptorDao.getStylesheetDescriptor(ssdId);
            final IStylesheetUserPreferences stylesheetUserPreferences = stylesheetUserPreferencesDao.createStylesheetUserPreferences(stylesheetDescriptor, person, userProfile);
            assertNotNull(stylesheetUserPreferences);
            stylesheetUserPreferences.setStylesheetParameter("activeTab", "1");
            stylesheetUserPreferences.setLayoutAttribute("u1l1n1", "deletable", "false");
            stylesheetUserPreferencesDao.storeStylesheetUserPreferences(stylesheetUserPreferences);
            return stylesheetUserPreferences.getId();
        }
    });
    this.execute(new Callable<Long>() {

        @Override
        public Long call() throws Exception {
            final IStylesheetUserPreferences stylesheetUserPreferences = stylesheetUserPreferencesDao.getStylesheetUserPreferences(supId);
            assertNotNull(stylesheetUserPreferences);
            assertEquals(Collections.singletonMap("activeTab", "1"), stylesheetUserPreferences.populateStylesheetParameters(new MapPopulator<String, String>()).getMap());
            assertEquals(Collections.singletonMap("deletable", "false"), stylesheetUserPreferences.populateLayoutAttributes("u1l1n1", new MapPopulator<String, String>()).getMap());
            return null;
        }
    });
    this.execute(new Callable<Long>() {

        @Override
        public Long call() throws Exception {
            final IStylesheetDescriptor stylesheetDescriptor = stylesheetDescriptorDao.getStylesheetDescriptor(ssdId);
            final IStylesheetUserPreferences stylesheetUserPreferences = stylesheetUserPreferencesDao.getStylesheetUserPreferences(stylesheetDescriptor, person, userProfile);
            assertNotNull(stylesheetUserPreferences);
            assertEquals(Collections.singletonMap("activeTab", "1"), stylesheetUserPreferences.populateStylesheetParameters(new MapPopulator<String, String>()).getMap());
            assertEquals(Collections.singletonMap("deletable", "false"), stylesheetUserPreferences.populateLayoutAttributes("u1l1n1", new MapPopulator<String, String>()).getMap());
            stylesheetUserPreferencesDao.deleteStylesheetUserPreferences(stylesheetUserPreferences);
            return null;
        }
    });
}
Also used : IPerson(org.apereo.portal.security.IPerson) IUserProfile(org.apereo.portal.IUserProfile) IStylesheetDescriptor(org.apereo.portal.layout.om.IStylesheetDescriptor) IStylesheetUserPreferences(org.apereo.portal.layout.om.IStylesheetUserPreferences) MapPopulator(org.apereo.portal.utils.MapPopulator) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Example 2 with MapPopulator

use of org.apereo.portal.utils.MapPopulator 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 3 with MapPopulator

use of org.apereo.portal.utils.MapPopulator 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 4 with MapPopulator

use of org.apereo.portal.utils.MapPopulator 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)4 IStylesheetUserPreferences (org.apereo.portal.layout.om.IStylesheetUserPreferences)4 MapPopulator (org.apereo.portal.utils.MapPopulator)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Element (org.w3c.dom.Element)2 NamedNodeMap (org.w3c.dom.NamedNodeMap)2 Locale (java.util.Locale)1 IUserProfile (org.apereo.portal.IUserProfile)1 StylesheetUserPreferencesImpl (org.apereo.portal.layout.StylesheetUserPreferencesImpl)1 IPerson (org.apereo.portal.security.IPerson)1 BasePortalJpaDaoTest (org.apereo.portal.test.BasePortalJpaDaoTest)1 Test (org.junit.Test)1