001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Thierry Delprat 018 */ 019package org.nuxeo.template.processors.xdocreport; 020 021import java.io.IOException; 022import java.io.StringWriter; 023 024import org.nuxeo.ecm.core.api.DocumentModel; 025import org.nuxeo.ecm.core.api.NuxeoException; 026import org.nuxeo.ecm.core.schema.DocumentType; 027import org.nuxeo.ecm.core.schema.SchemaManager; 028import org.nuxeo.ecm.core.schema.types.ComplexType; 029import org.nuxeo.ecm.core.schema.types.Field; 030import org.nuxeo.ecm.core.schema.types.Schema; 031import org.nuxeo.runtime.api.Framework; 032 033import fr.opensagres.xdocreport.template.TemplateEngineKind; 034import fr.opensagres.xdocreport.template.formatter.FieldsMetadata; 035 036/** 037 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 038 */ 039public class FieldDefinitionGenerator { 040 041 public static String generate(String type) { 042 SchemaManager sm = Framework.getLocalService(SchemaManager.class); 043 DocumentType docType = sm.getDocumentType(type); 044 045 return generate(docType.getSchemaNames()); 046 } 047 048 public static String generate(DocumentModel doc) { 049 return generate(doc.getSchemas()); 050 } 051 052 protected static String generate(String[] schemaNames) { 053 054 FieldsMetadata fieldsMetadata = new FieldsMetadata(TemplateEngineKind.Freemarker.name()); 055 056 for (String schemaName : schemaNames) { 057 SchemaManager sm = Framework.getLocalService(SchemaManager.class); 058 059 Schema schema = sm.getSchema(schemaName); 060 for (Field field : schema.getFields()) { 061 // String pname = field.getName().getPrefixedName(); 062 String name = field.getName().getLocalName(); 063 // String fieldName = "doc['" + name + "']"; 064 // fieldsMetadata.addField(fieldName, false, null, null, null); 065 String fieldName = "doc." + schemaName + "." + name; 066 if (field.getType().isListType()) { 067 fieldsMetadata.addField(fieldName, true, null, null, null); 068 } else { 069 fieldsMetadata.addField(fieldName, false, null, null, null); 070 if (field.getType().isComplexType()) { 071 072 ComplexType ct = (ComplexType) field.getType(); 073 if ("content".equals(ct.getName())) { 074 fieldsMetadata.addField(fieldName + ".filename", false, null, null, null); 075 } else { 076 for (Field subField : ct.getFields()) { 077 fieldsMetadata.addField(fieldName + "." + subField.getName().getLocalName(), false, 078 null, null, null); 079 } 080 } 081 } 082 } 083 } 084 } 085 086 fieldsMetadata.addField("doc.versionLabel", false, null, null, null); 087 fieldsMetadata.addField("doc.id", false, null, null, null); 088 fieldsMetadata.addField("doc.name", false, null, null, null); 089 fieldsMetadata.addField("doc.title", false, null, null, null); 090 fieldsMetadata.addField("doc.pathAsString", false, null, null, null); 091 fieldsMetadata.addField("doc.type", false, null, null, null); 092 fieldsMetadata.addField("doc.schemas", true, null, null, null); 093 fieldsMetadata.addField("doc.facets", true, null, null, null); 094 fieldsMetadata.addField("doc.locked", false, null, null, null); 095 fieldsMetadata.addField("doc.lockInfo", false, null, null, null); 096 fieldsMetadata.addField("doc.lockInfo.owner", false, null, null, null); 097 fieldsMetadata.addField("doc.lockInfo.created", false, null, null, null); 098 fieldsMetadata.addField("doc.checkedOut", false, null, null, null); 099 fieldsMetadata.addField("doc.", false, null, null, null); 100 101 // fieldsMetadata.load("principal", NuxeoPrincipal.class); // 102 // stackoverflow 103 104 fieldsMetadata.addField("principal.firstName", false, null, null, null); 105 fieldsMetadata.addField("principal.lastName", false, null, null, null); 106 fieldsMetadata.addField("principal.company", false, null, null, null); 107 fieldsMetadata.addField("principal.email", false, null, null, null); 108 fieldsMetadata.addField("principal.name", false, null, null, null); 109 110 fieldsMetadata.addField("auditEntries", false, null, null, null); 111 fieldsMetadata.addField("auditEntries.id", true, null, null, null); 112 fieldsMetadata.addField("auditEntries.principalName", true, null, null, null); 113 fieldsMetadata.addField("auditEntries.eventId", true, null, null, null); 114 fieldsMetadata.addField("auditEntries.eventDate", true, null, null, null); 115 fieldsMetadata.addField("auditEntries.docUUID", true, null, null, null); 116 fieldsMetadata.addField("auditEntries.docPath", true, null, null, null); 117 fieldsMetadata.addField("auditEntries.docType", true, null, null, null); 118 fieldsMetadata.addField("auditEntries.category", true, null, null, null); 119 fieldsMetadata.addField("auditEntries.comment", true, null, null, null); 120 fieldsMetadata.addField("auditEntries.docLifeCycle", true, null, null, null); 121 fieldsMetadata.addField("auditEntries.repositoryId", true, null, null, null); 122 123 StringWriter writer = new StringWriter(); 124 125 try { 126 fieldsMetadata.saveXML(writer, true); 127 } catch (IOException e) { 128 throw new NuxeoException(e); 129 } 130 131 return writer.getBuffer().toString(); 132 } 133}