001/*
002 * (C) Copyright 2008 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 *     Thierry Delprat
018 */
019package org.nuxeo.ecm.platform.exalead.ws;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Map;
025
026import javax.jws.WebMethod;
027import javax.jws.WebParam;
028import javax.jws.WebService;
029import javax.jws.soap.SOAPBinding;
030import javax.jws.soap.SOAPBinding.Style;
031
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DataModel;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.DocumentModelList;
036import org.nuxeo.ecm.core.api.IdRef;
037import org.nuxeo.ecm.core.api.IterableQueryResult;
038import org.nuxeo.ecm.core.api.impl.LifeCycleFilter;
039import org.nuxeo.ecm.core.query.sql.NXQL;
040import org.nuxeo.ecm.platform.api.ws.DocumentBlob;
041import org.nuxeo.ecm.platform.api.ws.DocumentDescriptor;
042import org.nuxeo.ecm.platform.api.ws.session.WSRemotingSession;
043import org.nuxeo.ecm.platform.exalead.ws.api.WSExalead;
044import org.nuxeo.ecm.platform.indexing.gateway.ws.UUIDPage;
045import org.nuxeo.ecm.platform.indexing.gateway.ws.WSIndexingGatewayBean;
046
047@WebService(name = "WSExaleadInterface", serviceName = "WSExaleadService")
048@SOAPBinding(style = Style.DOCUMENT)
049public class WSExaleadBean extends WSIndexingGatewayBean implements WSExalead {
050
051    /**
052     *
053     */
054    private static final long serialVersionUID = 87687687681L;
055
056    @Override
057    @WebMethod
058    public DocumentDescriptor[] getChildren(@WebParam(name = "sessionId") String sessionId,
059            @WebParam(name = "uuid") String uuid) {
060        WSRemotingSession rs = initSession(sessionId);
061        LifeCycleFilter filter = new LifeCycleFilter("deleted", false);
062        DocumentModelList docList = rs.getDocumentManager().getChildren(new IdRef(uuid), null, null, filter, null);
063        DocumentDescriptor[] docs = new DocumentDescriptor[docList.size()];
064        int i = 0;
065        for (DocumentModel doc : docList) {
066            docs[i++] = new DocumentDescriptor(doc);
067        }
068        return docs;
069    }
070
071    @Override
072    @WebMethod
073    public UUIDPage getRecursiveChildrenUUIDsByPage(@WebParam(name = "sessionId") String sid,
074            @WebParam(name = "uuid") String uuid, @WebParam(name = "page") int page,
075            @WebParam(name = "pageSize") int pageSize) {
076
077        CoreSession session = initSession(sid).getDocumentManager();
078
079        List<String> uuids = new ArrayList<String>();
080        IdRef parentRef = new IdRef(uuid);
081        DocumentModel parent = session.getDocument(parentRef);
082        String path = parent.getPathAsString();
083
084        String query = "select ecm:uuid from Document where ecm:path startswith '" + path
085                + " AND ecm:currentLifeCycleState != 'deleted' order by ecm:uuid";
086
087        IterableQueryResult result = session.queryAndFetch(query, "NXQL");
088        boolean hasMore = false;
089        try {
090            if (page > 1) {
091                int skip = (page - 1) * pageSize;
092                result.skipTo(skip);
093            }
094
095            for (Map<String, Serializable> record : result) {
096                uuids.add((String) record.get(NXQL.ECM_UUID));
097                if (uuids.size() == pageSize) {
098                    hasMore = true;
099                    break;
100                }
101            }
102        } finally {
103            result.close();
104        }
105        return new UUIDPage(uuids.toArray(new String[uuids.size()]), page, hasMore);
106    }
107
108    @Override
109    @WebMethod
110    public String[] getRecursiveChildrenUUIDs(@WebParam(name = "sessionId") String sid,
111            @WebParam(name = "uuid") String uuid) {
112
113        CoreSession session = initSession(sid).getDocumentManager();
114
115        List<String> uuids = new ArrayList<String>();
116        IdRef parentRef = new IdRef(uuid);
117        DocumentModel parent = session.getDocument(parentRef);
118        String path = parent.getPathAsString();
119
120        String query = "select ecm:uuid from Document where ecm:path startswith '" + path
121                + "'  AND ecm:currentLifeCycleState != 'deleted' order by ecm:uuid";
122
123        IterableQueryResult result = session.queryAndFetch(query, "NXQL");
124
125        try {
126            for (Map<String, Serializable> record : result) {
127                uuids.add((String) record.get(NXQL.ECM_UUID));
128            }
129        } finally {
130            result.close();
131        }
132
133        return uuids.toArray(new String[uuids.size()]);
134    }
135
136}