001/*
002 * (C) Copyright 2006-2007 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 *     Thomas Roger
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webapp.action;
023
024import static org.jboss.seam.ScopeType.CONVERSATION;
025
026import java.io.Serializable;
027import java.util.ArrayList;
028import java.util.List;
029
030import org.apache.commons.lang.StringUtils;
031import org.apache.commons.logging.Log;
032import org.apache.commons.logging.LogFactory;
033import org.jboss.seam.annotations.In;
034import org.jboss.seam.annotations.Name;
035import org.jboss.seam.annotations.Scope;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.ecm.webapp.base.InputController;
039
040/**
041 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
042 */
043@Name("editorLinkActions")
044@Scope(CONVERSATION)
045public class EditorLinkActionsBean extends InputController implements EditorLinkActions, Serializable {
046
047    private static final long serialVersionUID = 1L;
048
049    private static final Log log = LogFactory.getLog(EditorLinkActionsBean.class);
050
051    private List<DocumentModel> resultDocuments;
052
053    private boolean hasSearchResults = false;
054
055    private String searchKeywords;
056
057    @In(create = true, required = false)
058    private CoreSession documentManager;
059
060    @Override
061    public boolean getHasSearchResults() {
062        return hasSearchResults;
063    }
064
065    @Override
066    public List<DocumentModel> getSearchDocumentResults() {
067        return resultDocuments;
068    }
069
070    @Override
071    public String searchDocuments() {
072        log.debug("Entering searchDocuments with keywords: " + searchKeywords);
073
074        resultDocuments = null;
075        final List<String> constraints = new ArrayList<String>();
076        if (searchKeywords != null) {
077            searchKeywords = searchKeywords.trim();
078            if (searchKeywords.length() > 0) {
079                if (!searchKeywords.equals("*")) {
080                    // full text search
081                    constraints.add(String.format("ecm:fulltext LIKE '%s'", searchKeywords));
082                }
083            }
084        }
085        // no folderish doc nor hidden doc
086        constraints.add("ecm:mixinType != 'Folderish'");
087        constraints.add("ecm:mixinType != 'HiddenInNavigation'");
088        // no archived revisions
089        constraints.add("ecm:isCheckedInVersion = 0");
090        // search keywords
091        final String query = String.format("SELECT * FROM Document WHERE %s",
092                StringUtils.join(constraints.toArray(), " AND "));
093        log.debug("Query: " + query);
094
095        resultDocuments = documentManager.query(query, 100);
096        hasSearchResults = !resultDocuments.isEmpty();
097        log.debug("query result contains: " + resultDocuments.size() + " docs.");
098        return "editor_link_search_document";
099    }
100
101    @Override
102    public String getSearchKeywords() {
103        return searchKeywords;
104    }
105
106    @Override
107    public void setSearchKeywords(String searchKeywords) {
108        this.searchKeywords = searchKeywords;
109    }
110
111}