public StatusBean createContents(final String principalName, final Object[] inputsArray/*List contents*/)
{
if(!ServerNodeController.getController().getIsIPAllowed(getRequest()))
{
logger.error("A client with IP " + getRequest().getRemoteAddr() + " was denied access to the webservice. Could be a hack attempt or you have just not configured the allowed IP-addresses correct.");
return new StatusBean(false, "You are not allowed to talk to this service");
}
StatusBean statusBean = new StatusBean(true, "ok");
List<Integer> newContentIdList = new ArrayList<Integer>();
logger.info("****************************************");
logger.info("Creating contents through webservice....");
logger.info("****************************************");
logger.info("principalName:" + principalName);
logger.info("inputsArray:" + inputsArray);
//logger.warn("contents:" + contents);
try
{
final DynamicWebserviceSerializer serializer = new DynamicWebserviceSerializer();
@SuppressWarnings("unchecked")
List<Map<String, Object>> contents = (List<Map<String, Object>>) serializer.deserialize(inputsArray);
logger.info("contents:" + contents);
initializePrincipal(principalName);
Iterator<Map<String, Object>> contentIterator = contents.iterator();
while(contentIterator.hasNext())
{
//String value = (String)contentIterator.next();
//logger.info("value:" + value);
Map<String, Object> content = contentIterator.next();
String name = (String)content.get("name");
Integer contentTypeDefinitionId = (Integer)content.get("contentTypeDefinitionId");
Integer repositoryId = (Integer)content.get("repositoryId");
Integer parentContentId = (Integer)content.get("parentContentId");
String contentPath = (String)content.get("contentPath");
String creatorName = (String)content.get("creatorName");
Boolean isBranch = (Boolean)content.get("isBranch");
Integer isProtected = (Integer)content.get("isProtected");
Date expireDateTime = null;
Object expireDateTimeObject = content.get("expireDateTime");
if(expireDateTimeObject != null)
{
if(expireDateTimeObject instanceof Date)
expireDateTime = (Date)expireDateTimeObject;
else if(expireDateTimeObject instanceof Calendar)
expireDateTime = ((Calendar)expireDateTimeObject).getTime();
}
Date publishDateTime = null;
Object publishDateTimeObject = content.get("publishDateTime");
if(publishDateTimeObject != null)
{
if(publishDateTimeObject instanceof Date)
publishDateTime = (Date)publishDateTimeObject;
else if(publishDateTimeObject instanceof Calendar)
publishDateTime = ((Calendar)publishDateTimeObject).getTime();
}
logger.info("name:" + name);
logger.info("contentTypeDefinitionId:" + contentTypeDefinitionId);
logger.info("repositoryId:" + repositoryId);
logger.info("parentContentId:" + parentContentId);
logger.info("contentPath:" + contentPath);
if(contentPath != null && !contentPath.equals(""))
{
Database db = CastorDatabaseService.getDatabase();
beginTransaction(db);
try
{
if(parentContentId != null)
{
StringBuffer path = new StringBuffer();
Content parentContent = ContentController.getContentController().getContentWithId(parentContentId, db);
path.insert(0, parentContent.getName() + "/");
while(parentContent.getParentContent() != null)
{
parentContent = parentContent.getParentContent();
if(parentContent != null && parentContent.getParentContent() != null)
path.insert(0, parentContent.getName() + "/");
}
contentPath = path.toString() + contentPath;
}
ContentVO parentContentVO = ContentController.getContentController().getContentVOWithPath(repositoryId, contentPath, true, this.principal, db);
parentContentId = parentContentVO.getId();
commitTransaction(db);
}
catch(Exception e)
{
logger.error("An error occurred so we should not complete the transaction:" + e, e);
rollbackTransaction(db);
throw new SystemException(e.getMessage());
}
}
ContentVO contentVO = new ContentVO();
contentVO.setName(name);
contentVO.setContentTypeDefinitionId(contentTypeDefinitionId);
contentVO.setRepositoryId(repositoryId);
contentVO.setParentContentId(parentContentId);
if(expireDateTime != null)
contentVO.setExpireDateTime(expireDateTime);
if(isBranch != null)
contentVO.setIsBranch(isBranch);
if(isProtected != null)
contentVO.setIsProtected(isProtected);
if(publishDateTime != null)
contentVO.setPublishDateTime(publishDateTime);
if(creatorName != null)
contentVO.setCreatorName(creatorName);
else if(contentVO.getCreatorName() == null)
contentVO.setCreatorName(this.principal.getName());
ContentVO newContentVO = contentControllerProxy.acCreate(this.principal, contentVO.getParentContentId(), contentVO.getContentTypeDefinitionId(), contentVO.getRepositoryId(), contentVO);
if(newContentVO != null)
statusBean.getCreatedBeans().add(new CreatedEntityBean(ContentVO.class.getName(), new Long(newContentVO.getId())));
@SuppressWarnings("unchecked")
List<Map<String, Object>> contentVersions = (List<Map<String, Object>>)content.get("contentVersions");
if (contentVersions == null)
{
logger.info("Content had no versions. Maybe its a folder. IsBranch: " + isBranch);
}
else
{
Iterator<Map<String, Object>> contentVersionIterator = contentVersions.iterator();
while(contentVersionIterator.hasNext())
{
Map<String, Object> contentVersion = contentVersionIterator.next();
Integer languageId = (Integer)contentVersion.get("languageId");
Integer stateId = (Integer)contentVersion.get("stateId");
Boolean allowHTMLContent = (Boolean)contentVersion.get("allowHTMLContent");
Boolean allowExternalLinks = (Boolean)contentVersion.get("allowExternalLinks");
Boolean allowDollarSigns = (Boolean)contentVersion.get("allowDollarSigns");
Boolean allowAnchorSigns = (Boolean)contentVersion.get("allowAnchorSigns");
if(allowHTMLContent == null)
allowHTMLContent = new Boolean(false);
if(allowExternalLinks == null)
allowExternalLinks = new Boolean(false);
if(allowDollarSigns == null)
allowDollarSigns = new Boolean(false);
if(allowAnchorSigns == null)
allowAnchorSigns = new Boolean(true);
logger.info("languageId:" + languageId);
logger.info("stateId:" + stateId);
logger.info("allowHTMLContent:" + allowHTMLContent);
logger.info("allowExternalLinks:" + allowExternalLinks);
logger.info("allowDollarSigns:" + allowDollarSigns);
logger.info("allowAnchorSigns:" + allowAnchorSigns);
ContentVersionVO contentVersionVO = new ContentVersionVO();
contentVersionVO.setLanguageId(languageId);
if(contentVersionVO.getVersionModifier() == null)
contentVersionVO.setVersionModifier(this.principal.getName());
@SuppressWarnings("unchecked")
Map<String, String> attributes = (Map<String, String>)contentVersion.get("contentVersionAttributes");
DOMBuilder domBuilder = new DOMBuilder();
Document document = domBuilder.createDocument();
Element rootElement = domBuilder.addElement(document, "root");
domBuilder.addAttribute(rootElement, "xmlns", "x-schema:Schema.xml");
Element attributesRoot = domBuilder.addElement(rootElement, "attributes");
Iterator<String> attributesIterator = attributes.keySet().iterator();
while(attributesIterator.hasNext())
{
String attributeName = attributesIterator.next();
String attributeValue = attributes.get(attributeName);
attributeValue = cleanAttributeValue(attributeValue, allowHTMLContent, allowExternalLinks, allowDollarSigns, allowAnchorSigns);
Element attribute = domBuilder.addElement(attributesRoot, attributeName);
domBuilder.addCDATAElement(attribute, attributeValue);
}
contentVersionVO.setVersionValue(document.asXML());
ContentVersionVO newContentVersionVO = contentVersionControllerProxy.acCreate(this.principal, newContentVO.getId(), languageId, contentVersionVO);
Integer newContentVersionId = newContentVersionVO.getId();
statusBean.getCreatedBeans().add(new CreatedEntityBean(ContentVersionVO.class.getName(), new Long(newContentVersionId)));
@SuppressWarnings("unchecked")
List<RemoteAttachment> digitalAssets = (List<RemoteAttachment>)contentVersion.get("digitalAssets");
logger.info("digitalAssets:" + digitalAssets);
if(digitalAssets != null)
{
Iterator<RemoteAttachment> digitalAssetIterator = digitalAssets.iterator();
while(digitalAssetIterator.hasNext())
{
RemoteAttachment remoteAttachment = digitalAssetIterator.next();
logger.info("digitalAssets in ws:" + remoteAttachment);
DigitalAssetVO newAsset = new DigitalAssetVO();
newAsset.setAssetContentType(remoteAttachment.getContentType());
newAsset.setAssetKey(remoteAttachment.getName());
newAsset.setAssetFileName(remoteAttachment.getFileName());
newAsset.setAssetFilePath(remoteAttachment.getFilePath());
newAsset.setAssetFileSize(new Integer(new Long(remoteAttachment.getBytes().length).intValue()));
//is = new FileInputStream(renamedFile);
InputStream is = new ByteArrayInputStream(remoteAttachment.getBytes());
DigitalAssetVO newDigitalAssetVO = DigitalAssetController.create(newAsset, is, newContentVersionId, principal);
if(newDigitalAssetVO != null)
statusBean.getCreatedBeans().add(new CreatedEntityBean(DigitalAssetVO.class.getName(), new Long(newDigitalAssetVO.getId())));
}
}
@SuppressWarnings("unchecked")
List<String> contentCategories = (List<String>)contentVersion.get("contentCategories");
logger.info("contentCategories:" + contentCategories);
if(contentCategories != null)
{
Iterator<String> contentCategoriesIterator = contentCategories.iterator();
while(contentCategoriesIterator.hasNext())
{
String contentCategoryString = contentCategoriesIterator.next();
String[] split = contentCategoryString.split("=");
String categoryKey = split[0];
String fullCategoryName = split[1];
logger.info("categoryKey:" + categoryKey);
logger.info("fullCategoryName:" + fullCategoryName);
CategoryVO categoryVO = CategoryController.getController().findByPath(fullCategoryName);
logger.info("categoryVO:" + categoryVO);
List<CategoryVO> categoryVOList = new ArrayList<CategoryVO>();
categoryVOList.add(categoryVO);
Database db = beginTransaction();
try
{
ContentVersion latestContentVersion = ContentVersionController.getContentVersionController().getContentVersionWithId(newContentVersionVO.getId(), db);
logger.info("Adding categoryKey:" + categoryKey + " to " + newContentVersionVO.getId() + ":" + categoryVO);
//ContentCategoryController.getController().create(categoryVOList, newContentVersionVO, categoryKey);
final List<Category> categories = categoryVOListToCategoryList(categoryVOList, db);
ContentCategoryController.getController().create(categories, latestContentVersion, categoryKey, db);
commitTransaction(db);
}
catch (Exception e)
{
logger.warn("Error in contentCategory loop: " + e.getMessage(), e);
rollbackTransaction(db);
throw new SystemException(e.getMessage());
}
}
}
//Should we also change state on newly created content version?
if(stateId != null && !stateId.equals(ContentVersionVO.WORKING_STATE))
{
List<EventVO> events = new ArrayList<EventVO>();
ContentStateController.changeState(newContentVersionId, stateId, "Remote update from deliver", false, this.principal, newContentVO.getId(), events);
if(stateId.equals(ContentVersionVO.PUBLISHED_STATE))
{
PublicationVO publicationVO = new PublicationVO();
publicationVO.setName("Direct publication by " + this.principal.getName());
publicationVO.setDescription("Direct publication from deliver");
publicationVO.setRepositoryId(repositoryId);
publicationVO = PublicationController.getController().createAndPublish(publicationVO, events, false, this.principal);
}
}
}
}
@SuppressWarnings("unchecked")
List<Map<String, Object>> accessRights = (List<Map<String, Object>>)content.get("accessRights");
if(accessRights != null)
{
Iterator<Map<String, Object>> accessRightsIterator = accessRights.iterator();
while(accessRightsIterator.hasNext())
{
Map<String, Object> accessRightMap = accessRightsIterator.next();
String interceptionPointName = (String)accessRightMap.get("interceptionPointName");
String parameters = (String)accessRightMap.get("parameters");
Database db = CastorDatabaseService.getDatabase();
beginTransaction(db);
try
{
InterceptionPoint interceptionPoint = InterceptionPointController.getController().getInterceptionPointWithName(interceptionPointName, db);
logger.info("interceptionPointName:" + interceptionPointName);
logger.info("parameters:" + parameters);
AccessRightVO accessRightVO = new AccessRightVO();
accessRightVO.setParameters("" + newContentVO.getId());
AccessRight accessRight = AccessRightController.getController().create(accessRightVO, interceptionPoint, db);
@SuppressWarnings("unchecked")
List<String> accessRightRoles = (List<String>)accessRightMap.get("accessRightRoles");
if(accessRightRoles != null)
{
Iterator<String> accessRightRolesIterator = accessRightRoles.iterator();
while(accessRightRolesIterator.hasNext())
{
//Map accessRightRoleMap = (Map)accessRightRolesIterator.next();
String roleName = accessRightRolesIterator.next();
AccessRightRoleVO accessRightRoleVO = new AccessRightRoleVO();
accessRightRoleVO.setRoleName(roleName);
AccessRightRole accessRightRole = AccessRightController.getController().createAccessRightRole(db, accessRightRoleVO, accessRight);
@SuppressWarnings("unchecked")
Collection<AccessRightRole> roles = accessRight.getRoles();
roles.add(accessRightRole);
}
}
@SuppressWarnings("unchecked")
List<String> accessRightGroups = (List<String>)accessRightMap.get("accessRightGroups");
if(accessRightGroups != null)
{
Iterator<String> accessRightGroupsIterator = accessRightGroups.iterator();
while(accessRightGroupsIterator.hasNext())
{
String groupName = accessRightGroupsIterator.next();
AccessRightGroupVO accessRightGroupVO = new AccessRightGroupVO();
accessRightGroupVO.setGroupName(groupName);
AccessRightGroup accessRightGroup = AccessRightController.getController().createAccessRightGroup(db, accessRightGroupVO, accessRight);
@SuppressWarnings("unchecked")
Collection<AccessRightGroup> groups = accessRight.getGroups();
groups.add(accessRightGroup);
}
}
@SuppressWarnings("unchecked")
List<String> accessRightUsers = (List<String>)accessRightMap.get("accessRightUsers");
if(accessRightUsers != null)
{
Iterator<String> accessRightUsersIterator = accessRightUsers.iterator();
while(accessRightUsersIterator.hasNext())
{
String userName = accessRightUsersIterator.next();
AccessRightUserVO accessRightUserVO = new AccessRightUserVO();
accessRightUserVO.setUserName(userName);
AccessRightUser accessRightUser = AccessRightController.getController().createAccessRightUser(db, accessRightUserVO, accessRight);
@SuppressWarnings("unchecked")
Collection<AccessRightUser> users = accessRight.getUsers();
users.add(accessRightUser);
}
}
commitTransaction(db);
}
catch (Exception ex)
{
logger.error("An error occurred so we should not complete the transaction. Message: " + ex.getMessage());
logger.warn("An error occurred so we should not complete the transaction.", ex);
rollbackTransaction(db);
throw new SystemException(ex.getMessage());
}
}
}
newContentIdList.add(newContentVO.getId());
}
logger.info("Done with contents..");
}
catch(Throwable e)
{
statusBean.setStatus(false);
statusBean.setMessage("En error occurred when we tried to create a new content:" + e.getMessage());
logger.error("En error occurred when we tried to create a new content:" + e.getMessage());
logger.warn("En error occurred when we tried to create a new content:" + e.getMessage(), e);
}
updateCaches();