001/* 002 * (C) Copyright 2018 Nuxeo (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 * pierre 018 */ 019package org.nuxeo.ecm.platform.csv.export.io; 020 021import static org.nuxeo.ecm.core.io.registry.reflect.Instantiations.SINGLETON; 022import static org.nuxeo.ecm.core.io.registry.reflect.Priorities.REFERENCE; 023import static org.nuxeo.ecm.platform.csv.export.io.DocumentModelCSVHelper.getList; 024 025import java.io.IOException; 026import java.util.ArrayList; 027import java.util.Calendar; 028import java.util.Comparator; 029import java.util.List; 030 031import org.apache.commons.csv.CSVPrinter; 032import org.apache.commons.lang3.StringUtils; 033import org.nuxeo.ecm.core.api.DocumentModel; 034import org.nuxeo.ecm.core.api.Lock; 035import org.nuxeo.ecm.core.api.model.Property; 036import org.nuxeo.ecm.core.api.model.PropertyNotFoundException; 037import org.nuxeo.ecm.core.io.marshallers.csv.AbstractCSVWriter; 038import org.nuxeo.ecm.core.io.marshallers.csv.OutputStreamWithCSVWriter; 039import org.nuxeo.ecm.core.io.registry.Writer; 040import org.nuxeo.ecm.core.io.registry.reflect.Setup; 041import org.nuxeo.ecm.core.schema.types.Field; 042import org.nuxeo.ecm.core.schema.types.Schema; 043 044/** 045 * @since 10.3 046 */ 047@Setup(mode = SINGLETON, priority = REFERENCE) 048public class DocumentModelCSVWriter extends AbstractCSVWriter<DocumentModel> { 049 050 public static final String SCHEMAS_CTX_DATA = "schemas"; 051 052 public static final String XPATHS_CTX_DATA = "xpaths"; 053 054 public DocumentModelCSVWriter() { 055 super(); 056 } 057 058 @Override 059 protected void write(DocumentModel entity, CSVPrinter printer) throws IOException { 060 writeSystem(entity, printer); 061 for (String schemaName : getList(ctx, SCHEMAS_CTX_DATA)) { 062 Schema schema = schemaManager.getSchema(schemaName); 063 if (schema != null) { 064 writeSchema(entity, schema, printer); 065 } 066 } 067 for (String xpath : getList(ctx, XPATHS_CTX_DATA)) { 068 Field field = schemaManager.getField(xpath); 069 if (field != null) { 070 writeProperty(entity, xpath, printer); 071 } 072 } 073 } 074 075 @Override 076 protected void writeHeader(DocumentModel entity, CSVPrinter printer) throws IOException { 077 DocumentModelCSVHelper.printSystemPropertiesHeader(printer); 078 List<String> schemas = getList(ctx, SCHEMAS_CTX_DATA); 079 List<String> xpaths = getList(ctx, XPATHS_CTX_DATA); 080 DocumentModelCSVHelper.printPropertiesHeader(schemas, xpaths, printer); 081 printer.println(); 082 } 083 084 protected void writeSystem(DocumentModel doc, CSVPrinter printer) throws IOException { 085 printer.print(doc.getRepositoryName()); 086 printer.print(doc.getId()); 087 printer.print(doc.getPathAsString()); 088 printer.print(doc.getType()); 089 printer.print(doc.getRef() != null ? doc.getCurrentLifeCycleState() : null); 090 printer.print(doc.getParentRef() != null ? doc.getParentRef().toString() : null); 091 printer.print(doc.isCheckedOut()); 092 093 boolean isVersion = doc.isVersion(); 094 boolean isProxy = doc.isProxy(); 095 096 printer.print(doc.isVersion()); 097 printer.print(isProxy); 098 printer.print(isProxy ? doc.getSourceId() : null); 099 printer.print(isVersion || isProxy ? doc.getVersionSeriesId() : null); 100 printer.print(doc.getChangeToken()); 101 printer.print(doc.getRef() != null && doc.isTrashed()); 102 printer.print(doc.getTitle()); 103 printer.print(doc.getVersionLabel()); 104 Lock lock = doc.getLockInfo(); 105 if (lock != null) { 106 printer.print(lock.getOwner()); 107 printCalendar(lock.getCreated(), printer); 108 } else { 109 printer.print(null); 110 printer.print(null); 111 } 112 if (doc.hasSchema("dublincore")) { 113 Calendar cal = (Calendar) doc.getPropertyValue("dc:modified"); 114 printCalendar(cal, printer); 115 } else { 116 printer.print(null); 117 } 118 } 119 120 protected void writeSchema(DocumentModel entity, Schema schema, CSVPrinter printer) throws IOException { 121 // provides the current document to the property writer 122 List<Field> fields = new ArrayList<>(schema.getFields()); 123 // fields are sorted for reproducibility 124 fields.sort(Comparator.comparing(o -> o.getName().getLocalName())); 125 String prefix = schema.getNamespace().prefix; 126 if (StringUtils.isBlank(prefix)) { 127 prefix = schema.getName(); 128 } 129 prefix += ":"; 130 for (Field field : fields) { 131 writeProperty(entity, prefix + field.getName().getLocalName(), printer); 132 } 133 } 134 135 protected void writeProperty(DocumentModel entity, String xpath, CSVPrinter printer) throws IOException { 136 Writer<Property> propertyWriter = registry.getWriter(ctx, Property.class, TEXT_CSV_TYPE); 137 Property property = null; 138 try { 139 property = entity.getProperty(xpath); 140 } catch (PropertyNotFoundException e) { 141 // ignore 142 } 143 propertyWriter.write(property, Property.class, Property.class, TEXT_CSV_TYPE, 144 new OutputStreamWithCSVWriter(printer)); 145 } 146}