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.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.SystemPrincipal;
035import org.nuxeo.ecm.core.event.Event;
036import org.nuxeo.ecm.core.schema.SchemaManager;
037import org.nuxeo.runtime.api.Framework;
038import org.nuxeo.runtime.model.DefaultComponent;
039
040/**
041 * Service that writes Metadata.
042 *
043 * @author <a href="td@nuxeo.com">Thierry Delprat</a>
044 */
045public class DublinCoreStorageService extends DefaultComponent {
046
047    public static final String ID = "DublinCoreStorageService";
048
049    public void setCreationDate(DocumentModel doc, Calendar creationDate, Event event) {
050        doc.setProperty("dublincore", "created", creationDate);
051        addContributor(doc, event);
052    }
053
054    public void setModificationDate(DocumentModel doc, Calendar modificationDate, Event event) {
055        doc.setProperty("dublincore", "modified", modificationDate);
056        if (doc.getProperty("dublincore", "created") == null) {
057            setCreationDate(doc, modificationDate, event);
058        }
059    }
060
061    public void addContributor(DocumentModel doc, Event event) {
062        Principal principal = event.getContext().getPrincipal();
063        if (principal == null) {
064            return;
065        }
066
067        String principalName = principal.getName();
068        if (principal instanceof SystemPrincipal) {
069            SystemPrincipal nxp = (SystemPrincipal) principal;
070            String originatingUser = nxp.getOriginatingUser();
071            if ((originatingUser == null || SYSTEM_USERNAME.equals(originatingUser))
072                    && !DOCUMENT_CREATED.equals(event.getName())) {
073                return;
074            } else {
075                principalName = originatingUser;
076            }
077        }
078
079        String[] contributorsArray = (String[]) doc.getProperty("dublincore", "contributors");
080
081        List<String> contributorsList = new ArrayList<String>();
082
083        if (contributorsArray != null && contributorsArray.length > 0) {
084            contributorsList = Arrays.asList(contributorsArray);
085            // make it resizable
086            contributorsList = new ArrayList<String>(contributorsList);
087        } else {
088            // initialize creator too
089            SchemaManager schemaMgr = Framework.getService(SchemaManager.class);
090            if (schemaMgr.getSchema("dublincore").getField("creator") != null) {
091                // First time only => creator
092                doc.setProperty("dublincore", "creator", principalName);
093            }
094        }
095
096        if (!contributorsList.contains(principalName)) {
097            contributorsList.add(principalName);
098            String[] contributorListIn = new String[contributorsList.size()];
099            contributorsList.toArray(contributorListIn);
100            doc.setProperty("dublincore", "contributors", contributorListIn);
101        }
102
103        doc.setProperty("dublincore", "lastContributor", principalName);
104    }
105
106    public void setIssuedDate(DocumentModel doc, Calendar issuedDate) {
107        doc.setPropertyValue("dc:issued", issuedDate);
108    }
109
110}