boolean contentFragmentIsReference = (contentFragmentImpl.getReference() != null);
// retrieve current fragment and page or template from page manager
BaseFragmentsElement pageOrTemplate = getPageOrTemplate(contentFragmentImpl.getPageOrTemplate().getPath());
String pageFragmentId = (contentFragmentIsReference ? contentFragmentImpl.getReference().getId() : contentFragmentImpl.getFragment().getId());
BaseFragmentElement fragment = pageOrTemplate.getFragmentById(pageFragmentId);
if (fragment == null)
{
throw new IllegalArgumentException("Fragment and page not consistent");
}
// check for edit permission
fragment.checkAccess(JetspeedActions.EDIT);
// update fragment preferences and page in page manager
fragment.getPreferences().clear();
if (preferences != null)
{
Iterator preferencesIter = preferences.entrySet().iterator();
while (preferencesIter.hasNext())
{
Map.Entry preferencesEntry = (Map.Entry)preferencesIter.next();
FragmentPreference preference = pageManager.newFragmentPreference();
preference.setName((String)preferencesEntry.getKey());
Object values = preferencesEntry.getValue();
if (values instanceof String)
{
preference.getValueList().add((String)values);
}
else if (values instanceof String [])
{
preference.getValueList().addAll(Arrays.asList(((String [])values)));
}
else if (values instanceof FragmentPreference)
{
FragmentPreference fragmentPreference = (FragmentPreference)values;
preference.setReadOnly(fragmentPreference.isReadOnly());
preference.getValueList().addAll(fragmentPreference.getValueList());
}
else if (values instanceof PortletPreference)
{
PortletPreference portletPreference = (PortletPreference)values;
preference.setReadOnly(portletPreference.isReadOnly());
String [] prefValues = portletPreference.getValues();
if (prefValues != null)
{
preference.getValueList().addAll(Arrays.asList(prefValues));
}
}
else
{
throw new IllegalArgumentException("Unexpected preference value type");
}
fragment.getPreferences().add(preference);
}
}
updatePage(pageOrTemplate);
// update content context