001/*
002 * (C) Copyright 2010 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 *     Anahide Tchertchian
018 */
019package org.nuxeo.ecm.platform.smart.folder.jsf;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Map;
025
026import org.jboss.seam.ScopeType;
027import org.jboss.seam.annotations.In;
028import org.jboss.seam.annotations.Name;
029import org.jboss.seam.annotations.Scope;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.SortInfo;
032import org.nuxeo.ecm.platform.smart.query.jsf.SmartNXQLQueryActions;
033import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
034import org.nuxeo.ecm.webapp.contentbrowser.DocumentActions;
035import org.nuxeo.runtime.logging.DeprecationLogger;
036
037/**
038 * Provides methods to save a global smart search in a document.
039 *
040 * @author Anahide Tchertchian
041 * @since 5.4
042 * @deprecated since 8.1: use generic methods on SearchUIActionsBean
043 */
044@Deprecated
045@Name("smartNXQLFolderActions")
046@Scope(ScopeType.CONVERSATION)
047public class SmartNXQLFolderActions implements Serializable {
048
049    private static final long serialVersionUID = 1L;
050
051    @In(create = true, required = true)
052    protected DocumentActions documentActions;
053
054    @In(create = true, required = true)
055    protected transient NavigationContext navigationContext;
056
057    @In(create = true, required = true)
058    protected transient SmartNXQLQueryActions smartNXQLQueryActions;
059
060    /**
061     * Initializes a document model of given type, and fill its properties according to fields set on the seam component
062     * {@link SmartNXQLFolderActions}.
063     * <p>
064     * Assumes the document type holds the 'content_view_display' and 'smart_folder' schemas.
065     */
066    public String saveQueryAsDocument(String docType) {
067        DeprecationLogger.log(
068                "Seam component names 'smartNXQLFolderActions' is deprecated, use component named 'searchUIActions' instead.",
069                "8.1");
070        documentActions.createDocument(docType);
071        // fill in information from smart search
072        DocumentModel doc = navigationContext.getChangeableDocument();
073        String queryPart = smartNXQLQueryActions.getQueryPart();
074        List<String> selectedLayoutColumns = smartNXQLQueryActions.getSelectedLayoutColumns();
075        List<SortInfo> sortInfos = smartNXQLQueryActions.getSearchSortInfos();
076        doc.setPropertyValue(SmartFolderDocumentConstants.QUERY_PART_PROP_NAME, queryPart);
077        doc.setPropertyValue(SmartFolderDocumentConstants.SELECTED_LAYOUT_COLUMNS_PROP_NAME,
078                (Serializable) selectedLayoutColumns);
079        List<Map<String, Serializable>> sortInfosForDoc = new ArrayList<Map<String, Serializable>>();
080        if (sortInfos != null) {
081            for (SortInfo sortInfo : sortInfos) {
082                if (sortInfo != null) {
083                    sortInfosForDoc.add(SortInfo.asMap(sortInfo));
084                }
085            }
086        }
087        doc.setPropertyValue(SmartFolderDocumentConstants.SORT_INFOS_PROP_NAME, (Serializable) sortInfosForDoc);
088        return "save_smart_query_as_document";
089    }
090}