001/*
002 * (C) Copyright 2013 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 *     dmetzler
016 */
017package org.nuxeo.ecm.automation.io.services.enricher;
018
019import java.io.IOException;
020import java.util.List;
021
022import org.codehaus.jackson.JsonGenerator;
023import org.nuxeo.common.utils.StringUtils;
024import org.nuxeo.ecm.automation.jaxrs.io.documents.JsonDocumentListWriter;
025import org.nuxeo.ecm.automation.jaxrs.io.documents.JsonDocumentWriter;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentModelList;
029import org.nuxeo.ecm.core.io.marshallers.json.enrichers.ChildrenJsonEnricher;
030
031/**
032 * This enricher adds a document list with all children to the contextParameters map. It is provided as sample and may
033 * not be used directly. For instance if you have millions of children, it will get all of them.
034 *
035 * @since 5.7.3
036 * @deprecated This enricher was migrated to {@link ChildrenJsonEnricher}. The content enricher service doesn't work
037 *             anymore.
038 */
039@Deprecated
040public class ChildrenEnricher extends AbstractContentEnricher {
041
042    @Override
043    public void enrich(JsonGenerator jg, RestEvaluationContext ec) throws IOException {
044        DocumentModel doc = ec.getDocumentModel();
045        CoreSession session = doc.getCoreSession();
046        DocumentModelList children = session.getChildren(doc.getRef());
047        List<String> props = ec.getHeaders().getRequestHeader(JsonDocumentWriter.DOCUMENT_PROPERTIES_HEADER);
048        String[] schemas = null;
049        if (props != null && !props.isEmpty()) {
050            schemas = StringUtils.split(props.get(0), ',', true);
051        }
052        JsonDocumentListWriter.writeDocuments(jg, children, schemas, ec.getRequest());
053    }
054
055}