001/*
002 * (C) Copyright 2006-2011 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 *     bstefanescu
018 */
019package org.nuxeo.ecm.automation.core.operations.document;
020
021import java.util.Iterator;
022
023import org.apache.commons.lang.StringUtils;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.automation.core.util.StringList;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModelList;
032import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
033import org.nuxeo.ecm.core.query.sql.NXQL;
034
035/**
036 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
037 */
038@Operation(id = FetchByProperty.ID, category = Constants.CAT_FETCH, label = "Fetch By Property", description = "For each specified string property value, fetch all documents that match the property and the optional where clause. Matching documents are collected into a list and the returned to the next operation. The operation has no input.")
039public class FetchByProperty {
040
041    public static final String ID = "Document.FetchByProperty";
042
043    @Context
044    protected CoreSession session;
045
046    @Param(name = "property", required = true)
047    protected String property;
048
049    @Param(name = "values", required = true)
050    protected StringList values;
051
052    @Param(name = "query", required = false)
053    protected String query;
054
055    @OperationMethod
056    public DocumentModelList run() {
057        if (values.isEmpty()) {
058            return new DocumentModelListImpl();
059        }
060        if (StringUtils.isBlank(query)) {
061            query = null;
062        }
063        StringBuilder sb = new StringBuilder();
064        sb.append("SELECT * FROM Document WHERE ");
065        sb.append(property);
066        if (values.size() == 1) {
067            sb.append(" = ");
068            sb.append(NXQL.escapeString(values.get(0)));
069        } else {
070            sb.append(" IN (");
071            for (Iterator<String> it = values.iterator(); it.hasNext();) {
072                sb.append(NXQL.escapeString(it.next()));
073                if (it.hasNext()) {
074                    sb.append(", ");
075                }
076            }
077            sb.append(")");
078        }
079        if (query != null) {
080            sb.append(" AND (");
081            sb.append(query);
082            sb.append(")");
083        }
084        String q = sb.toString();
085        return session.query(q);
086    }
087
088}