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.restapi.server.jaxrs;
020
021import java.util.Map.Entry;
022
023import javax.ws.rs.DELETE;
024import javax.ws.rs.GET;
025import javax.ws.rs.PUT;
026import javax.ws.rs.core.Response;
027
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DataModel;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.DocumentModelList;
032import org.nuxeo.ecm.webengine.model.WebObject;
033import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
034
035/**
036 * Web object that allows to deal with documents list.
037 *
038 * @since 5.7.3
039 * @deprecated since 10.3, use {@link BulkActionFrameworkObject BAF} instead
040 */
041@WebObject(type = "bulk")
042@Deprecated
043public class BulkDocumentsObject extends DefaultObject {
044
045    protected DocumentModelList docs;
046
047    @Override
048    public <A> A getAdapter(Class<A> adapter) {
049        if (adapter == DocumentModel.class) {
050            return adapter.cast(docs);
051        }
052        return super.getAdapter(adapter);
053    }
054
055    @Override
056    public void initialize(Object... args) {
057        assert args != null && args.length == 1;
058        docs = (DocumentModelList) args[0];
059    }
060
061    @GET
062    public DocumentModelList doGet() {
063        return docs;
064    }
065
066    @DELETE
067    public Response doDelete() {
068        CoreSession session = getContext().getCoreSession();
069        for (DocumentModel doc : docs) {
070            session.removeDocument(doc.getRef());
071        }
072        session.save();
073        return Response.ok().build();
074    }
075
076    @PUT
077    public DocumentModelList doUpdate(DocumentModel updateDoc) {
078        CoreSession session = getContext().getCoreSession();
079
080        for (DocumentModel doc : docs) {
081            updateDirtyFields(updateDoc, doc);
082            session.saveDocument(doc);
083        }
084        session.save();
085        return docs;
086    }
087
088    /**
089     * Copy the dirty fields of srcDoc to dstDoc.
090     *
091     * @since 5.7.3
092     */
093    private void updateDirtyFields(DocumentModel srcDoc, DocumentModel dstDoc) {
094        for (Entry<String, DataModel> entry : srcDoc.getDataModels().entrySet()) {
095            String schemaName = entry.getKey();
096            for (String field : entry.getValue().getDirtyFields()) {
097                Object value = srcDoc.getDataModel(schemaName).getValue(field);
098                dstDoc.getDataModel(schemaName).setValue(field, value);
099            }
100        }
101    }
102
103}