alfresco自定义内容模型,使用自定义模型上传文件。
通过自定义内容模型,扩展内容属性,注入业务需要的相关属性。
1、定义内容模型文件
例如 StudentContentModel.xml
<?xml version="1.0" encoding="UTF-8"?>
<model name="yy:student" xmlns="http://www.alfresco.org/model/dictionary/1.0">
<description>Student Content Model</description>
<author>yuyong</author>
<version>1.0</version>
<imports>
//导入模型字典定义(字段类型的定义)
<import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d"/>
<import uri="http://www.alfresco.org/model/content/1.0" prefix="cm"/>
</imports>
<namespaces>
//内容模型的命名空间
<namespace uri="yy.student.model" prefix="yy"/>
</namespaces>
<aspects>
//定义内容自定义属性
<aspect name="yy:studentAspect">
<title>The Student Content Model aspect</title>
<properties>
<property name="yy:name">
<type>d:text</type>
</property>
<property name="yy:age">
<type>d:int</type>
</property>
</properties>
</aspect>
</aspects>
</model>
2、修改custom-model-content.xml文件,引入定义的内容模型文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
<bean id="custom.dictionaryBootstrap" parent="dictionaryModelBootstrap" depends-on="dictionaryBootstrap">
<property name="models">
<list>
<value>alfresco/extension/StudentContentModel.xml</value>
</list>
</property>
</bean>
</beans>
3、修改web-client-config-custom.xml文件,添加客户端显示的内容模型属性
<alfresco-config>
<config evaluator="aspect-name" condition="yy:studentAspect">
<property-sheet>
<show-property name="yy:name"/>
<show-property name="yy:age"/>
</property-sheet>
</config>
<config evaluator="string-compare" condition="Action Wizards">
<aspects>
<aspect name="yy:studentAspect"/>
</aspects>
</config>
</alfresco-config>
将三个文件放入 alfresco\tomcat\shared\classes\alfresco\extension中
编程上传文件,使用自定义的内容模型,使用自定义的扩展属性
alfresco-webservice:
通过alfresco-webservice在company_home space下面的student的space中,根据自定义StudentContentModel内容模型上传文件,并且赋予自定义属性
startSession();
Store STORE = new Store(Constants.WORKSPACE_STORE,
"SpacesStore");
ParentReference companyHomeParent = new ParentReference(
STORE, null, "/app:company_home",
Constants.ASSOC_CONTAINS, null);
Reference space = new Reference(STORE, null, companyHomeParent.getPath()
+ "/cm:" + "student");
String docname ="upload.doc";
Reference document = null;
//内容模型的域
String domain="{yy.student.model}";
//内容模型属性集合
String aspect_name="articleAspect";
ParentReference parent = new ParentReference();
parent.setStore(STORE);
parent.setPath(space.getPath());
parent.setUuid(space.getUuid());
parent.setAssociationType(Constants.ASSOC_CONTAINS);
parent.setChildName(Constants.createQNameString(
Constants.NAMESPACE_CONTENT_MODEL, docname));
NamedValue[] properties = new NamedValue[] { Utils.createNamedValue(
Constants.PROP_NAME, docname) };
CMLCreate create = new CMLCreate("1", parent, null, null, null,
Constants.TYPE_CONTENT, properties);
NamedValue[] aspect_properties = new NamedValue[2];
aspect_properties[0] = Utils.createNamedValue(domain+"name", "yuyong");
aspect_properties[1] = Utils.createNamedValue(domain+"age", "23");
CMLAddAspect addaspect = new CMLAddAspect();
addaspect.setAspect(domain+aspect_name);
addaspect.setProperty(aspect_properties);
addaspect
.setWhere(new Predicate(new Reference[] { parent }, null, null));
addaspect.setWhere_id("1");
CML cml = new CML();
cml.setCreate(new CMLCreate[] { create });
cml.setAddAspect(new CMLAddAspect[] { addaspect });
UpdateResult[] results = null;
try {
results = getRepositoryService().update(cml);
document = results[0].getDestination();
} catch (Exception e) {
System.err.println("Can not create the document");
throw e;
}
//以上是在student space下面定义一个项目的reference,并且根据内容模型的定义赋予自定义属性值。此时,此reference还没有文件内容。仅仅项目属性的赋值。
//以下是上面已经定义的项目中写入文件内容。格式需要正确,写入后,可以通过 alfresco web application查看到上传的文件
byte[] documentStream=getDocumentStream();
ContentFormat format = new ContentFormat("application/msword","UTF-8");
try {
WebServiceFactory.getContentService().write(document, Constants.PROP_CONTENT,
documentStream, format);
} catch (Exception e) {
System.err.println("Can not set the content of the document");
throw e;
}
endSession();