001/* 002 * (C) Copyright 2006-2011 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 * Nuxeo - initial API and implementation 018 * 019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $ 020 */ 021 022package org.nuxeo.ecm.platform.dublincore.service; 023 024import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED; 025import static org.nuxeo.ecm.core.api.security.SecurityConstants.SYSTEM_USERNAME; 026 027import java.security.Principal; 028import java.util.ArrayList; 029import java.util.Arrays; 030import java.util.Calendar; 031import java.util.List; 032 033import org.apache.commons.logging.Log; 034import org.apache.commons.logging.LogFactory; 035import org.nuxeo.ecm.core.api.DocumentModel; 036import org.nuxeo.ecm.core.api.SystemPrincipal; 037import org.nuxeo.ecm.core.event.Event; 038import org.nuxeo.ecm.core.schema.SchemaManager; 039import org.nuxeo.runtime.api.Framework; 040import org.nuxeo.runtime.model.DefaultComponent; 041 042/** 043 * Service that writes Metadata. 044 * 045 * @author <a href="td@nuxeo.com">Thierry Delprat</a> 046 */ 047public class DublinCoreStorageService extends DefaultComponent { 048 049 public static Log log = LogFactory.getLog(DublinCoreStorageService.class); 050 051 public static final String ID = "DublinCoreStorageService"; 052 053 public void setCreationDate(DocumentModel doc, Calendar creationDate, Event event) { 054 doc.setProperty("dublincore", "created", creationDate); 055 addContributor(doc, event); 056 } 057 058 public void setModificationDate(DocumentModel doc, Calendar modificationDate, Event event) { 059 doc.setProperty("dublincore", "modified", modificationDate); 060 if (doc.getProperty("dublincore", "created") == null) { 061 setCreationDate(doc, modificationDate, event); 062 } 063 } 064 065 public void addContributor(DocumentModel doc, Event event) { 066 Principal principal = event.getContext().getPrincipal(); 067 if (principal == null) { 068 return; 069 } 070 071 String principalName = principal.getName(); 072 if (principal instanceof SystemPrincipal) { 073 SystemPrincipal nxp = (SystemPrincipal) principal; 074 String originatingUser = nxp.getOriginatingUser(); 075 if ((originatingUser == null || SYSTEM_USERNAME.equals(originatingUser)) 076 && !DOCUMENT_CREATED.equals(event.getName())) { 077 return; 078 } else { 079 principalName = originatingUser; 080 } 081 } 082 083 String[] contributorsArray = (String[]) doc.getProperty("dublincore", "contributors"); 084 085 List<String> contributorsList = new ArrayList<String>(); 086 087 if (contributorsArray != null && contributorsArray.length > 0) { 088 contributorsList = Arrays.asList(contributorsArray); 089 // make it resizable 090 contributorsList = new ArrayList<String>(contributorsList); 091 } else { 092 // initialize creator too 093 SchemaManager schemaMgr = Framework.getLocalService(SchemaManager.class); 094 if (schemaMgr.getSchema("dublincore").getField("creator") != null) { 095 // First time only => creator 096 doc.setProperty("dublincore", "creator", principalName); 097 } 098 } 099 100 if (!contributorsList.contains(principalName)) { 101 contributorsList.add(principalName); 102 String[] contributorListIn = new String[contributorsList.size()]; 103 contributorsList.toArray(contributorListIn); 104 doc.setProperty("dublincore", "contributors", contributorListIn); 105 } 106 107 doc.setProperty("dublincore", "lastContributor", principalName); 108 } 109 110 public void setIssuedDate(DocumentModel doc, Calendar issuedDate) { 111 doc.setPropertyValue("dc:issued", issuedDate); 112 } 113 114}