use of org.apereo.portal.layout.om.IStylesheetUserPreferences 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;
}
use of org.apereo.portal.layout.om.IStylesheetUserPreferences 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);
}
}
use of org.apereo.portal.layout.om.IStylesheetUserPreferences in project uPortal by Jasig.
the class StylesheetUserPreferencesServiceImpl method getAllLayoutAttributeNodeIds.
@Override
public Iterable<String> getAllLayoutAttributeNodeIds(HttpServletRequest request, PreferencesScope prefScope) {
final StylesheetPreferencesKey stylesheetPreferencesKey = this.getStylesheetPreferencesKey(request, prefScope);
final LinkedHashSet<String> allNodeIds = new LinkedHashSet<String>();
final IStylesheetUserPreferences stylesheetUserPreferences = this.getStylesheetUserPreferences(request, stylesheetPreferencesKey);
if (stylesheetUserPreferences != null) {
allNodeIds.addAll(stylesheetUserPreferences.getAllLayoutAttributeNodeIds());
}
final HttpSession session = request.getSession(false);
if (session != null) {
final Map<String, Map<String, String>> sessionLayoutAttributes = getSessionLayoutAttributes(session, stylesheetPreferencesKey);
if (sessionLayoutAttributes != null) {
allNodeIds.addAll(sessionLayoutAttributes.keySet());
}
}
final Map<String, Map<String, String>> requestLayoutAttributes = getRequestLayoutAttributes(request, stylesheetPreferencesKey);
if (requestLayoutAttributes != null) {
allNodeIds.addAll(requestLayoutAttributes.keySet());
}
final IStylesheetUserPreferences distributedStylesheetUserPreferences = this.getDistributedStylesheetUserPreferences(request, prefScope);
if (distributedStylesheetUserPreferences != null) {
allNodeIds.addAll(distributedStylesheetUserPreferences.getAllLayoutAttributeNodeIds());
}
return allNodeIds;
}
use of org.apereo.portal.layout.om.IStylesheetUserPreferences 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<String, String>();
this.putDataValue(request, stylesheetPreferencesKey, scope, LAYOUT_ATTRIBUTES_KEY, nodeId, nodeAttributes);
}
}
return nodeAttributes.put(name, value);
}
}
}
use of org.apereo.portal.layout.om.IStylesheetUserPreferences in project uPortal by Jasig.
the class StylesheetUserPreferencesServiceImpl method getLayoutAttribute.
@Override
public String getLayoutAttribute(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 get layout attribute {} for ID=\"{}\" but no such stylesheet parameter is defined in stylesheet descriptor {}. Null will be returned", new Object[] { name, nodeId, stylesheetDescriptor.getName() });
return null;
}
//Load the default value
String defaultValue = null;
final IStylesheetUserPreferences distributedStylesheetUserPreferences = this.getDistributedStylesheetUserPreferences(request, prefScope);
if (distributedStylesheetUserPreferences != null) {
defaultValue = distributedStylesheetUserPreferences.getLayoutAttribute(nodeId, name);
// special handling for fragment owner
if (this.isFragmentOwnerStylesheetPreferencesKey(stylesheetPreferencesKey)) {
return defaultValue;
}
if (this.compareValues(defaultValue, layoutAttributeDescriptor.getDefaultValue())) {
//DLM attribute value matches the stylesheet descriptor default, remove the DLM value
distributedStylesheetUserPreferences.removeLayoutAttribute(nodeId, name);
defaultValue = null;
}
}
final String value;
final Scope scope = layoutAttributeDescriptor.getScope();
switch(scope) {
case PERSISTENT:
{
final IStylesheetUserPreferences stylesheetUserPreferences = this.getStylesheetUserPreferences(request, stylesheetPreferencesKey);
if (stylesheetUserPreferences == null) {
value = null;
break;
}
value = stylesheetUserPreferences.getLayoutAttribute(nodeId, name);
break;
}
default:
{
final Map<String, String> nodeAttributes = this.getDataValue(request, stylesheetPreferencesKey, scope, LAYOUT_ATTRIBUTES_KEY, nodeId);
if (nodeAttributes == null) {
value = null;
break;
}
value = nodeAttributes.get(name);
break;
}
}
if (value == null) {
return defaultValue;
}
if (!this.isFragmentOwnerStylesheetPreferencesKey(stylesheetPreferencesKey)) {
if (this.compareValues(value, layoutAttributeDescriptor.getDefaultValue()) || //Value is equal to stylesheet descriptor default
(defaultValue != null && this.compareValues(value, defaultValue))) {
//Value is equal to DLM stylesheet preferences default
//Remove the user's customized value
this.removeLayoutAttribute(request, prefScope, nodeId, name);
return null;
}
}
return value;
}
Aggregations