String iCatID = id.startsWith("cat") ? id.substring(3) : id;
categories[iter.previousIndex()] = channelRegistryStore
.getChannelCategory(iCatID);
}
IChannelDefinition channelDef = channelRegistryStore.getChannelDefinition(form.getId());
if (channelDef == null) {
final String fname = form.getFname();
final String clazz = form.getJavaClass();
final String name = form.getName();
final String title = form.getTitle();
final int typeId = form.getTypeId();
channelDef = channelRegistryStore.newChannelDefinition(typeId, fname, clazz, name, title);
}
channelDef.setDescription(form.getDescription());
channelDef.setEditable(form.isEditable());
channelDef.setFName(form.getFname());
channelDef.setHasAbout(form.isHasAbout());
channelDef.setHasHelp(form.isHasHelp());
channelDef.setIsSecure(form.isSecure());
channelDef.setJavaClass(form.getJavaClass());
channelDef.setName(form.getName());
channelDef.setTimeout(form.getTimeout());
channelDef.setTitle(form.getTitle());
Date now = new Date();
int order = form.getLifecycleState().getOrder();
if (form.getId() < 0) {
if (order >= ChannelLifecycleState.APPROVED.getOrder()) {
channelDef.setApproverId(publisher.getID());
channelDef.setApprovalDate(now);
}
if (order >= ChannelLifecycleState.PUBLISHED.getOrder()) {
channelDef.setPublisherId(publisher.getID());
if (channelDef.getPublishDate() == null) {
channelDef.setPublishDate(now);
}
} else if (form.getPublishDate() != null) {
channelDef.setPublishDate(form.getPublishDateTime());
channelDef.setPublisherId(publisher.getID());
}
if (order >= ChannelLifecycleState.EXPIRED.getOrder()) {
channelDef.setExpirerId(publisher.getID());
if (channelDef.getExpirationDate() == null) {
channelDef.setExpirationDate(now);
}
} else if (form.getExpirationDate() != null) {
channelDef.setExpirationDate(form.getExpirationDateTime());
channelDef.setExpirerId(publisher.getID());
}
}
// if we're updating a channel
else {
if (order >= ChannelLifecycleState.APPROVED.getOrder()) {
if (channelDef.getApproverId() < 0) {
channelDef.setApproverId(publisher.getID());
}
if (channelDef.getApprovalDate() == null) {
channelDef.setApprovalDate(now);
}
} else {
channelDef.setApprovalDate(null);
channelDef.setApproverId(-1);
}
if (order >= ChannelLifecycleState.PUBLISHED.getOrder()) {
if (channelDef.getPublisherId() < 0) {
channelDef.setPublisherId(publisher.getID());
}
if (channelDef.getPublishDate() == null) {
channelDef.setPublishDate(now);
}
} else if (form.getPublishDate() != null) {
channelDef.setPublishDate(form.getPublishDate());
if (channelDef.getPublisherId() < 0) {
channelDef.setPublisherId(publisher.getID());
}
} else {
channelDef.setPublishDate(null);
channelDef.setPublisherId(-1);
}
if (order >= ChannelLifecycleState.EXPIRED.getOrder()) {
if (channelDef.getExpirerId() < 0) {
channelDef.setExpirerId(publisher.getID());
}
if (channelDef.getExpirationDate() == null) {
channelDef.setExpirationDate(now);
}
} else if (form.getExpirationDate() != null) {
channelDef.setExpirationDate(form.getExpirationDate());
if (channelDef.getExpirerId() < 0) {
channelDef.setExpirerId(publisher.getID());
}
} else {
channelDef.setExpirationDate(null);
channelDef.setExpirerId(-1);
}
}
final IChannelType channelType = channelRegistryStore.getChannelType(form.getTypeId());
if (channelType == null) {
throw new IllegalArgumentException("No IChannelType exists for ID " + form.getTypeId());
}
channelDef.setType(channelType);
// add channel parameters
List<IPortletPreference> preferenceList = new ArrayList<IPortletPreference>();
for (String key : form.getParameters().keySet()) {
String value = form.getParameters().get(key).getValue();
if (!StringUtils.isBlank(value)) {
boolean override = false;
if (form.getParameterOverrides().containsKey(key)) {
override = form.getParameterOverrides().get(key).getValue();
}
if (key.startsWith("PORTLET.")) {
preferenceList.add(new PortletPreferenceImpl(key, !override, new String[]{value}));
} else {
channelDef.addParameter(key, value, override);
}
}
}
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()]);
boolean readOnly = true;
if (form.getPortletPreferencesOverrides().containsKey(key)) {
readOnly = !form.getPortletPreferencesOverrides().get(key).getValue();
}
preferenceList.add(new PortletPreferenceImpl(key, readOnly, values));
}
}
final IPortletDefinition portletDefinition = this.portletDefinitionRegistry.getPortletDefinition(channelDef.getId());
final IPortletPreferences portletPreferences = portletDefinition.getPortletPreferences();
portletPreferences.setPortletPreferences(preferenceList);
channelPublishingService.saveChannelDefinition(channelDef, publisher, categories, groupMembers);
return this.getChannelDefinitionForm(channelDef.getId());
}