001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.automation.core.operations.document;
013
014import java.util.Iterator;
015
016import org.apache.commons.lang.StringUtils;
017import org.nuxeo.ecm.automation.core.Constants;
018import org.nuxeo.ecm.automation.core.annotations.Context;
019import org.nuxeo.ecm.automation.core.annotations.Operation;
020import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
021import org.nuxeo.ecm.automation.core.annotations.Param;
022import org.nuxeo.ecm.automation.core.util.StringList;
023import org.nuxeo.ecm.core.api.CoreSession;
024import org.nuxeo.ecm.core.api.DocumentModelList;
025import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl;
026import org.nuxeo.ecm.core.query.sql.NXQL;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031@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.")
032public class FetchByProperty {
033
034    public static final String ID = "Document.FetchByProperty";
035
036    @Context
037    protected CoreSession session;
038
039    @Param(name = "property", required = true)
040    protected String property;
041
042    @Param(name = "values", required = true)
043    protected StringList values;
044
045    @Param(name = "query", required = false)
046    protected String query;
047
048    @OperationMethod
049    public DocumentModelList run() {
050        if (values.isEmpty()) {
051            return new DocumentModelListImpl();
052        }
053        if (StringUtils.isBlank(query)) {
054            query = null;
055        }
056        StringBuilder sb = new StringBuilder();
057        sb.append("SELECT * FROM Document WHERE ");
058        sb.append(property);
059        if (values.size() == 1) {
060            sb.append(" = ");
061            sb.append(NXQL.escapeString(values.get(0)));
062        } else {
063            sb.append(" IN (");
064            for (Iterator<String> it = values.iterator(); it.hasNext();) {
065                sb.append(NXQL.escapeString(it.next()));
066                if (it.hasNext()) {
067                    sb.append(", ");
068                }
069            }
070            sb.append(")");
071        }
072        if (query != null) {
073            sb.append(" AND (");
074            sb.append(query);
075            sb.append(")");
076        }
077        String q = sb.toString();
078        return session.query(q);
079    }
080
081}