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 if (values.containsKey(NXQL.ECM_LIFECYCLESTATE)) { 058 doc.putContextData(INITIAL_LIFECYCLE_STATE_OPTION_NAME, values.get(NXQL.ECM_LIFECYCLESTATE)); 059 values.remove(NXQL.ECM_LIFECYCLESTATE); 060 } 061 doc = session.createDocument(doc); 062 for (Map.Entry<String, Serializable> entry : values.entrySet()) { 063 doc.setPropertyValue(entry.getKey(), entry.getValue()); 064 } 065 session.saveDocument(doc); 066 } 067 068 protected Map<String, Serializable> prepareValues(Map<String, Serializable> values) { 069 if (values.containsKey(DC_CREATOR)) { 070 // make sure the creator is part of the contributors 071 String creator = (String) values.get(DC_CREATOR); 072 String[] contributorsArray = (String[]) values.get(DC_CONTRIBUTORS); 073 List<String> contributors = contributorsArray == null ? new ArrayList<>() : new ArrayList<>( 074 Arrays.asList(contributorsArray)); 075 if (StringUtils.isNotBlank(creator) && !contributors.contains(creator)) { 076 contributors.add(creator); 077 } 078 values.put(DC_CONTRIBUTORS, contributors.toArray(new String[contributors.size()])); 079 } 080 return values; 081 } 082 083 @Override 084 public void updateDocument(CoreSession session, DocumentRef docRef, Map<String, Serializable> values) { 085 DocumentModel doc = session.getDocument(docRef); 086 for (Map.Entry<String, Serializable> entry : values.entrySet()) { 087 if (!IGNORE_FIELDS_ON_UPDATE.contains(entry.getKey())) { 088 doc.setPropertyValue(entry.getKey(), entry.getValue()); 089 } 090 } 091 session.saveDocument(doc); 092 } 093 094 @Override 095 public boolean exists(CoreSession session, String parentPath, String name, 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 101 @Override 102 @Deprecated 103 public boolean exists(CoreSession session, String parentPath, String name, String type, 104 Map<String, Serializable> values) { 105 return exists(session, parentPath, name, null); 106 } 107}