001/*
002 * (C) Copyright 2014 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-2.1.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 *     Vladimir Pasquier <vpasquier@nuxeo.com>
016 */
017package org.nuxeo.ecm.automation.core.impl.adapters;
018
019import org.nuxeo.ecm.automation.OperationContext;
020import org.nuxeo.ecm.automation.TypeAdaptException;
021import org.nuxeo.ecm.automation.TypeAdapter;
022import org.nuxeo.ecm.core.api.DocumentModelList;
023import org.nuxeo.ecm.core.api.DocumentNotFoundException;
024import org.nuxeo.ecm.core.api.DocumentRef;
025import org.nuxeo.ecm.core.api.IdRef;
026import org.nuxeo.ecm.core.api.PathRef;
027import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
028
029/**
030 * @since 6.0
031 */
032public class ArrayStringToDocModelList implements TypeAdapter {
033
034    @Override
035    public Object getAdaptedValue(OperationContext ctx, Object objectToAdapt) throws TypeAdaptException {
036        String[] content = (String[]) objectToAdapt;
037        DocumentModelList result = new DocumentModelListImpl();
038        try {
039            for (String value : content) {
040                result.add(ctx.getCoreSession().getDocument(createRef(value)));
041            }
042        } catch (DocumentNotFoundException e) {
043            throw new TypeAdaptException(e);
044        }
045        return result;
046    }
047
048    public DocumentRef createRef(String value) {
049        return value.startsWith("/") ? new PathRef(value) : new IdRef(value);
050    }
051
052}