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 * Thomas Roger 018 */ 019 020package org.nuxeo.ecm.csv; 021 022import static org.nuxeo.ecm.core.api.LifeCycleConstants.INITIAL_LIFECYCLE_STATE_OPTION_NAME; 023 024import java.io.Serializable; 025import java.util.ArrayList; 026import java.util.Arrays; 027import java.util.List; 028import java.util.Map; 029 030import org.apache.commons.lang.StringUtils; 031import org.nuxeo.common.utils.Path; 032import org.nuxeo.ecm.core.api.CoreSession; 033import org.nuxeo.ecm.core.api.DocumentModel; 034import org.nuxeo.ecm.core.api.DocumentRef; 035import org.nuxeo.ecm.core.api.PathRef; 036import org.nuxeo.ecm.core.query.sql.NXQL; 037 038/** 039 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 040 * @since 5.7 041 */ 042public class DefaultCSVImporterDocumentFactory implements CSVImporterDocumentFactory { 043 044 private static final long serialVersionUID = 1L; 045 046 public static final String DC_CREATOR = "dc:creator"; 047 048 public static final String DC_CONTRIBUTORS = "dc:contributors"; 049 050 public static final List<String> IGNORE_FIELDS_ON_UPDATE = Arrays.asList(NXQL.ECM_LIFECYCLESTATE); 051 052 @Override 053 public void createDocument(CoreSession session, String parentPath, String name, String type, 054 Map<String, Serializable> values) { 055 values = prepareValues(values); 056 DocumentModel doc = session.createDocumentModel(parentPath, name, type); 057 for (Map.Entry<String, Serializable> entry : values.entrySet()) { 058 if (NXQL.ECM_LIFECYCLESTATE.equals(entry.getKey())) { 059 doc.putContextData(INITIAL_LIFECYCLE_STATE_OPTION_NAME, entry.getValue()); 060 } else { 061 doc.setPropertyValue(entry.getKey(), entry.getValue()); 062 } 063 } 064 session.createDocument(doc); 065 } 066 067 protected Map<String, Serializable> prepareValues(Map<String, Serializable> values) { 068 if (values.containsKey(DC_CREATOR)) { 069 // make sure the creator is part of the contributors 070 String creator = (String) values.get(DC_CREATOR); 071 String[] contributorsArray = (String[]) values.get(DC_CONTRIBUTORS); 072 List<String> contributors = contributorsArray == null ? new ArrayList<>() : new ArrayList<>( 073 Arrays.asList(contributorsArray)); 074 if (StringUtils.isNotBlank(creator) && !contributors.contains(creator)) { 075 contributors.add(creator); 076 } 077 values.put(DC_CONTRIBUTORS, contributors.toArray(new String[contributors.size()])); 078 } 079 return values; 080 } 081 082 @Override 083 public void updateDocument(CoreSession session, DocumentRef docRef, Map<String, Serializable> values) { 084 DocumentModel doc = session.getDocument(docRef); 085 for (Map.Entry<String, Serializable> entry : values.entrySet()) { 086 if (!IGNORE_FIELDS_ON_UPDATE.contains(entry.getKey())) { 087 doc.setPropertyValue(entry.getKey(), entry.getValue()); 088 } 089 } 090 session.saveDocument(doc); 091 } 092 093 @Override 094 public boolean exists(CoreSession session, String parentPath, String name, String type, 095 Map<String, Serializable> values) { 096 String targetPath = new Path(parentPath).append(name).toString(); 097 DocumentRef docRef = new PathRef(targetPath); 098 return session.exists(docRef); 099 } 100}