{
List componentPropertyDefinitions = ComponentPropertyDefinitionController.getController().getComponentPropertyDefinitions(db, componentContentId, masterLanguageId);
Iterator componentPropertyDefinitionsIterator = componentPropertyDefinitions.iterator();
while(componentPropertyDefinitionsIterator.hasNext())
{
ComponentPropertyDefinition componentPropertyDefinition = (ComponentPropertyDefinition)componentPropertyDefinitionsIterator.next();
logger.info("componentPropertyDefinition:" + componentPropertyDefinition);
if(componentPropertyDefinition.getAutoCreateContent())
{
logger.info("componentPropertyDefinition vill ha en auto create:" + componentPropertyDefinition);
String method = componentPropertyDefinition.getAutoCreateContentMethod();
String path = componentPropertyDefinition.getAutoCreateContentPath();
String allowedContentTypeNames = componentPropertyDefinition.getAllowedContentTypeNames();
String allowedContentTypeName = allowedContentTypeNames.split(",")[0];
ContentTypeDefinitionVO createContentTypeDefinitionVO = ContentTypeDefinitionController.getController().getContentTypeDefinitionVOWithName(allowedContentTypeName, db);
logger.info("method:" + method);
logger.info("path:" + path);
logger.info("createContentTypeDefinitionVO:" + createContentTypeDefinitionVO);
if(createContentTypeDefinitionVO == null || method == null)
continue;
if(path.indexOf("/") == 0)
path = path.substring(1);
SiteNodeVO siteNodeVO = SiteNodeController.getController().getSiteNodeVOWithId(siteNodeId, db);
ContentVO parentContentVO = ContentController.getContentController().getRootContent(db, siteNodeVO.getRepositoryId(), principal.getName(), true).getValueObject();
if(method.equals("siteStructure"))
{
String siteNodePath = SiteNodeController.getController().getSiteNodePath(siteNodeVO.getId(), db);
logger.info("siteNodePath:" + siteNodePath);
parentContentVO = ContentController.getContentController().getContentVOWithPath(siteNodeVO.getRepositoryId(), path + siteNodePath, true, principal, db);
}
else if(method.equals("fixedPath"))
{
parentContentVO = ContentController.getContentController().getContentVOWithPath(siteNodeVO.getRepositoryId(), path, true, principal, db);
}
ContentVO autoContentVO = new ContentVO();
autoContentVO.setName(siteNodeVO.getName() + " - " + componentPropertyDefinition.getName());
autoContentVO.setCreatorName(principal.getName());
autoContentVO.setIsBranch(false);
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><article xmlns=\"x-schema:ArticleSchema.xml\"><attributes>");
List contentTypeAttributes = ContentTypeDefinitionController.getController().getContentTypeAttributes(createContentTypeDefinitionVO, true);
Iterator contentTypeAttributesIterator = contentTypeAttributes.iterator();
while(contentTypeAttributesIterator.hasNext())
{
ContentTypeAttribute contentTypeAttribute = (ContentTypeAttribute)contentTypeAttributesIterator.next();
String initialValue = null;
if(contentTypeAttribute.getContentTypeAttribute("initialData") != null && contentTypeAttribute.getContentTypeAttribute("initialData").getContentTypeAttributeParameterValue() != null)
{
initialValue = contentTypeAttribute.getContentTypeAttribute("initialData").getContentTypeAttributeParameterValue().getValue("label");
}
boolean skip = false;
if(contentTypeAttribute.getContentTypeAttribute("enableRelationEditor") != null && contentTypeAttribute.getContentTypeAttribute("enableRelationEditor").getContentTypeAttributeParameterValue() != null && contentTypeAttribute.getContentTypeAttribute("enableRelationEditor").getContentTypeAttributeParameterValue().getValue("label").equals("true"))
{
skip = true;
}
if(contentTypeAttribute.getContentTypeAttribute("enableContentRelationEditor") != null && contentTypeAttribute.getContentTypeAttribute("enableContentRelationEditor").getContentTypeAttributeParameterValue() != null && contentTypeAttribute.getContentTypeAttribute("enableContentRelationEditor").getContentTypeAttributeParameterValue().getValue("label").equals("true"))
{
skip = true;
}
if(contentTypeAttribute.getContentTypeAttribute("enableStructureRelationEditor") != null && contentTypeAttribute.getContentTypeAttribute("enableStructureRelationEditor").getContentTypeAttributeParameterValue() != null && contentTypeAttribute.getContentTypeAttribute("enableStructureRelationEditor").getContentTypeAttributeParameterValue().getValue("label").equals("true"))
{
skip = true;
}
if(!skip)
{
if(initialValue == null || initialValue.trim().equals(""))
initialValue = "State " + contentTypeAttribute.getName();
sb.append("<" + contentTypeAttribute.getName() + "><![CDATA[" + initialValue + "]]></" + contentTypeAttribute.getName() + ">");
}
else
logger.info("Skipping:" + contentTypeAttribute.getName());
}
sb.append("</attributes></article>");
String defaultValue = sb.toString();
ContentVersionVO autoContentVersionVO = new ContentVersionVO();
autoContentVersionVO.setVersionComment("Automatically created");
autoContentVersionVO.setVersionModifier(principal.getName());
autoContentVersionVO.setVersionValue(defaultValue);
autoContentVO = ContentController.getContentController().create(db, parentContentVO.getId(), createContentTypeDefinitionVO.getId(), siteNodeVO.getRepositoryId(), autoContentVO).getValueObject();
ContentVersionController.getContentVersionController().create(autoContentVO.getId(), languageId, autoContentVersionVO, null, db);
Locale locale = LanguageController.getController().getLocaleWithId(languageId, db);
LanguageVO masterLanguageVO = LanguageController.getController().getMasterLanguage(siteNodeVO.getRepositoryId(), db);
String entity = "Content";
Integer entityId = new Integer(autoContentVO.getId());
String propertyName = "" + componentPropertyDefinition.getName();
ComponentController.getController().addComponentPropertyBinding(document, locale, siteNodeId, languageId, masterLanguageVO.getId(), entity, entityId, propertyName, newComponentId, path, assetKey, principal);
}
}
}