}
if (!form.isNew()) {
// User must have the previous lifecycle permission
// in AT LEAST ONE previous category as well
IPortletDefinition def = this.portletDefinitionRegistry.getPortletDefinition(form.getId());
Set<PortletCategory> categories = portletCategoryRegistry.getParentCategories(def);
List<JsonEntityBean> categoryBeans = new ArrayList<JsonEntityBean>();
for (PortletCategory cat : categories) {
categoryBeans.add(new JsonEntityBean(cat));
}
if (!hasLifecyclePermission(publisher, def.getLifecycleState(), categoryBeans)) {
logger.warn("User '" + publisher.getUserName() +
"' attempted to save the following portlet without the previous MANAGE permission: " + form);
throw new SecurityException("Not Authorized");
}
}
if (form.isNew() || portletDefinitionRegistry.getPortletDefinition(form.getId()).getType().getId() != form.getTypeId()) {
// User must have access to the selected CPD if s/he selected it in this interaction
final int selectedTypeId = form.getTypeId();
final PortletPublishingDefinition cpd = portletPublishingDefinitionDao.getChannelPublishingDefinition(selectedTypeId);
final Map<IPortletType, PortletPublishingDefinition> allowableCpds = this.getAllowableChannelPublishingDefinitions(publisher);
if (!allowableCpds.containsValue(cpd)) {
logger.warn("User '" + publisher.getUserName() +
"' attempted to administer the following portlet without the selected " +
IPermission.PORTLET_MANAGER_SELECT_PORTLET_TYPE + " permission: " + form);
throw new SecurityException("Not Authorized");
}
}
// create the group array from the form's group list
IGroupMember[] groupMembers = new IGroupMember[form.getGroups().size()];
for (int i = 0; i < groupMembers.length; i++) {
JsonEntityBean bean = form.getGroups().get(i);
EntityEnum entityEnum = EntityEnum.getEntityEnum(bean.getEntityTypeAsString());
if (entityEnum.isGroup()) {
groupMembers[i] = GroupService.findGroup(bean.getId());
} else {
groupMembers[i] = GroupService.getGroupMember(bean.getId(), entityEnum.getClazz());
}
}
// create the category array from the form's category list
PortletCategory[] categories = new PortletCategory[form.getCategories().size()];
for (ListIterator<JsonEntityBean> iter = form.getCategories().listIterator(); iter.hasNext();) {
String id = iter.next().getId();
String iCatID = id.startsWith("cat") ? id.substring(3) : id;
categories[iter.previousIndex()] = portletCategoryRegistry.getPortletCategory(iCatID);
}
IPortletDefinition portletDef = null;
if (form.getId() == null) {
final String fname = form.getFname();
final String name = form.getName();
final String title = form.getTitle();
final String applicationId = form.getApplicationId();
final String portletName = form.getPortletName();
final boolean isFramework = form.isFramework();
final IPortletType type = portletTypeRegistry.getPortletType(form.getTypeId());
portletDef = portletDefinitionRegistry.createPortletDefinition(type, fname, name, title, applicationId, portletName, isFramework);
} else {
portletDef = portletDefinitionRegistry.getPortletDefinition(form.getId());
}
portletDef.setDescription(form.getDescription());
portletDef.setFName(form.getFname());
portletDef.setName(form.getName());
portletDef.setTimeout(form.getTimeout());
portletDef.setTitle(form.getTitle());
portletDef.getPortletDescriptorKey().setWebAppName(form.getApplicationId());
portletDef.getPortletDescriptorKey().setPortletName(form.getPortletName());
portletDef.getPortletDescriptorKey().setFrameworkPortlet(form.isFramework());
final IPortletType portletType = portletTypeRegistry.getPortletType(form.getTypeId());
if (portletType == null) {
throw new IllegalArgumentException("No IPortletType exists for ID " + form.getTypeId());
}
portletDef.setType(portletType);
// Make parameters (NB: these are different from preferences) in the
// portletDef reflect the state of the form, in case any have changed.
for (String key : form.getParameters().keySet()) {
String value = form.getParameters().get(key).getValue();
if (!StringUtils.isBlank(value)) {
portletDef.addParameter(key, value);
}
}
portletDef.addParameter(IPortletDefinition.EDITABLE_PARAM, Boolean.toString(form.isEditable()));
portletDef.addParameter(IPortletDefinition.CONFIGURABLE_PARAM, Boolean.toString(form.isConfigurable()));
portletDef.addParameter(IPortletDefinition.HAS_HELP_PARAM, Boolean.toString(form.isHasHelp()));
portletDef.addParameter(IPortletDefinition.HAS_ABOUT_PARAM, Boolean.toString(form.isHasAbout()));
// Now add portlet preferences
List<IPortletPreference> preferenceList = new ArrayList<IPortletPreference>();
for (String key : form.getPortletPreferences().keySet()) {
List<String> prefValues = form.getPortletPreferences().get(key).getValue();
if (prefValues != null && prefValues.size() > 0) {
String[] values = prefValues.toArray(new String[prefValues.size()]);
BooleanAttribute readOnly = form.getPortletPreferenceReadOnly().get(key);
preferenceList.add(new PortletPreferenceImpl(key, readOnly.getValue(), values));
}
}
portletDef.setPortletPreferences(preferenceList);
// Lastly update the PortletDefinition's lifecycle state & lifecycle-related metadata
updateLifecycleState(form, portletDef, publisher);
portletPublishingService.savePortletDefinition(portletDef, publisher, Arrays.asList(categories), Arrays.asList(groupMembers));
return this.getPortletDefinitionForm(publisher, portletDef.getPortletDefinitionId().getStringId());
}