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