001/* 002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Thomas Roger 016 */ 017 018package org.nuxeo.ecm.csv; 019 020import static org.nuxeo.ecm.core.api.LifeCycleConstants.INITIAL_LIFECYCLE_STATE_OPTION_NAME; 021 022import java.io.Serializable; 023import java.util.Arrays; 024import java.util.List; 025import java.util.Map; 026 027import org.nuxeo.common.utils.Path; 028import org.nuxeo.ecm.core.api.CoreSession; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.DocumentRef; 031import org.nuxeo.ecm.core.api.PathRef; 032import org.nuxeo.ecm.core.query.sql.NXQL; 033 034/** 035 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a> 036 * @since 5.7 037 */ 038public class DefaultCSVImporterDocumentFactory implements CSVImporterDocumentFactory { 039 040 private static final long serialVersionUID = 1L; 041 042 public static final List<String> IGNORE_FIELDS_ON_UPDATE = Arrays.asList(NXQL.ECM_LIFECYCLESTATE); 043 044 @Override 045 public void createDocument(CoreSession session, String parentPath, String name, String type, 046 Map<String, Serializable> values) { 047 DocumentModel doc = session.createDocumentModel(parentPath, name, type); 048 for (Map.Entry<String, Serializable> entry : values.entrySet()) { 049 if (NXQL.ECM_LIFECYCLESTATE.equals(entry.getKey())) { 050 doc.putContextData(INITIAL_LIFECYCLE_STATE_OPTION_NAME, entry.getValue()); 051 } else { 052 doc.setPropertyValue(entry.getKey(), entry.getValue()); 053 } 054 } 055 session.createDocument(doc); 056 } 057 058 @Override 059 public void updateDocument(CoreSession session, DocumentRef docRef, Map<String, Serializable> values) 060 { 061 DocumentModel doc = session.getDocument(docRef); 062 for (Map.Entry<String, Serializable> entry : values.entrySet()) { 063 if (!IGNORE_FIELDS_ON_UPDATE.contains(entry.getKey())) { 064 doc.setPropertyValue(entry.getKey(), entry.getValue()); 065 } 066 } 067 session.saveDocument(doc); 068 } 069 070 @Override 071 public boolean exists(CoreSession session, String parentPath, String name, String type, 072 Map<String, Serializable> values) { 073 String targetPath = new Path(parentPath).append(name).toString(); 074 DocumentRef docRef = new PathRef(targetPath); 075 return session.exists(docRef); 076 } 077}