001/*
002 * (C) Copyright 2002-2013 Nuxeo SAS (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 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.ecm.platform.importer.xml.parser;
020
021import java.text.DateFormat;
022import java.text.ParseException;
023import java.text.SimpleDateFormat;
024import java.util.ArrayList;
025import java.util.Calendar;
026import java.util.Date;
027import java.util.List;
028import java.util.Map;
029import java.util.Stack;
030
031import org.dom4j.Element;
032import org.nuxeo.ecm.automation.core.scripting.CoreFunctions;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.DocumentNotFoundException;
036
037/**
038 * Some helper function that are injected inside MVEL context
039 *
040 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
041 */
042public class MVELImporterFunction extends CoreFunctions {
043
044    protected final CoreSession session;
045
046    protected final Stack<DocumentModel> docsStack;
047
048    protected final Map<Element, DocumentModel> elToDoc;
049
050    protected final Element el;
051
052    public MVELImporterFunction(CoreSession session, Stack<DocumentModel> docsStack,
053            Map<Element, DocumentModel> elToDoc, Element el) {
054        super();
055        this.session = session;
056        this.docsStack = docsStack;
057        this.elToDoc = elToDoc;
058        this.el = el;
059    }
060
061    public Calendar parseDate(String source, String format) throws ParseException {
062        DateFormat df = new SimpleDateFormat(format);
063        Date date = df.parse(source);
064        Calendar result = Calendar.getInstance();
065        result.setTime(date);
066        return result;
067    }
068
069    public DocumentModel mkdir(DocumentModel parent, String regexp, String data, String typeName)
070            {
071
072        String[] parts = data.split(regexp);
073        List<DocumentModel> result = new ArrayList<DocumentModel>();
074        DocumentModel root = parent;
075
076        for (String part : parts) {
077            DocumentModel child = null;
078            try {
079                child = session.getChild(root.getRef(), part);
080            } catch (DocumentNotFoundException e) {
081                child = session.createDocumentModel(root.getPathAsString(), part, typeName);
082                child.setPropertyValue("dc:title", part);
083                child = session.createDocument(child);
084            }
085            result.add(child);
086            docsStack.push(child);
087            root = child;
088        }
089
090        if (result.size() > 0) {
091            elToDoc.put(el, result.get(result.size() - 1));
092            return result.get(result.size() - 1);
093        }
094        return null;
095    }
096
097}