001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webapp.action;
021
022import static org.jboss.seam.ScopeType.CONVERSATION;
023
024import java.io.Serializable;
025import java.util.ArrayList;
026import java.util.List;
027
028import org.apache.commons.lang.StringUtils;
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.jboss.seam.annotations.In;
032import org.jboss.seam.annotations.Name;
033import org.jboss.seam.annotations.Scope;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.webapp.base.InputController;
037
038/**
039 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
040 */
041@Name("editorLinkActions")
042@Scope(CONVERSATION)
043public class EditorLinkActionsBean extends InputController implements EditorLinkActions, Serializable {
044
045    private static final long serialVersionUID = 1L;
046
047    private static final Log log = LogFactory.getLog(EditorLinkActionsBean.class);
048
049    private List<DocumentModel> resultDocuments;
050
051    private boolean hasSearchResults = false;
052
053    private String searchKeywords;
054
055    @In(create = true, required = false)
056    private CoreSession documentManager;
057
058    @Override
059    public boolean getHasSearchResults() {
060        return hasSearchResults;
061    }
062
063    @Override
064    public List<DocumentModel> getSearchDocumentResults() {
065        return resultDocuments;
066    }
067
068    @Override
069    public String searchDocuments() {
070        log.debug("Entering searchDocuments with keywords: " + searchKeywords);
071
072        resultDocuments = null;
073        final List<String> constraints = new ArrayList<String>();
074        if (searchKeywords != null) {
075            searchKeywords = searchKeywords.trim();
076            if (searchKeywords.length() > 0) {
077                if (!searchKeywords.equals("*")) {
078                    // full text search
079                    constraints.add(String.format("ecm:fulltext LIKE '%s'", searchKeywords));
080                }
081            }
082        }
083        // no folderish doc nor hidden doc
084        constraints.add("ecm:mixinType != 'Folderish'");
085        constraints.add("ecm:mixinType != 'HiddenInNavigation'");
086        // no archived revisions
087        constraints.add("ecm:isCheckedInVersion = 0");
088        // search keywords
089        final String query = String.format("SELECT * FROM Document WHERE %s",
090                StringUtils.join(constraints.toArray(), " AND "));
091        log.debug("Query: " + query);
092
093        resultDocuments = documentManager.query(query, 100);
094        hasSearchResults = !resultDocuments.isEmpty();
095        log.debug("query result contains: " + resultDocuments.size() + " docs.");
096        return "editor_link_search_document";
097    }
098
099    @Override
100    public String getSearchKeywords() {
101        return searchKeywords;
102    }
103
104    @Override
105    public void setSearchKeywords(String searchKeywords) {
106        this.searchKeywords = searchKeywords;
107    }
108
109}