001/*
002 * (C) Copyright 2016 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 *     Gabriel Barata <gbarata@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.search.core;
020
021import java.io.IOException;
022import java.security.Principal;
023import java.util.Map;
024
025import org.apache.commons.lang.StringUtils;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentRef;
029import org.nuxeo.ecm.core.api.IdRef;
030import org.nuxeo.ecm.core.api.security.ACE;
031import org.nuxeo.ecm.core.api.security.ACL;
032import org.nuxeo.ecm.core.api.security.ACP;
033import org.nuxeo.ecm.core.api.security.SecurityConstants;
034import org.nuxeo.ecm.platform.query.api.PageProviderService;
035import org.nuxeo.ecm.platform.userworkspace.api.UserWorkspaceService;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * @since 8.3
040 */
041public class SavedSearchServiceImpl implements SavedSearchService {
042
043    @Override
044    public SavedSearch createSavedSearch(CoreSession session, String title, String queryParams,
045            Map<String, String> namedParams, String query, String queryLanguage, String pageProviderName,
046            Long pageSize, Long currentPageIndex, Long maxResults, String sortBy, String sortOrder,
047            String contentViewData) throws InvalidSearchParameterException, IOException {
048        if (StringUtils.isEmpty(title)) {
049            throw new InvalidSearchParameterException("title cannot be empty");
050        }
051
052        if ((!StringUtils.isEmpty(query) || !StringUtils.isEmpty(queryLanguage))
053                && !StringUtils.isEmpty(pageProviderName)) {
054            throw new InvalidSearchParameterException("query and page provider parameters are mutually exclusive"
055                    + " (query, queryLanguage, pageProviderName)");
056        }
057
058        if (StringUtils.isEmpty(query) && StringUtils.isEmpty(queryLanguage)
059                && StringUtils.isEmpty(pageProviderName)) {
060            throw new InvalidSearchParameterException("query or page provider parameters are missing"
061                    + " (query, queryLanguage, pageProviderName)");
062        }
063
064        if (!StringUtils.isEmpty(query) && StringUtils.isEmpty(queryLanguage)) {
065            throw new InvalidSearchParameterException("queryLanguage parameter is missing");
066        }
067
068        if (StringUtils.isEmpty(query) && !StringUtils.isEmpty(queryLanguage)) {
069            throw new InvalidSearchParameterException("query parameter is missing");
070        }
071
072        UserWorkspaceService userWorkspaceService = Framework.getService(UserWorkspaceService.class);
073        DocumentModel uws = userWorkspaceService.getCurrentUserPersonalWorkspace(session, null);
074
075        String searchDocumentType = (!StringUtils.isEmpty(pageProviderName)) ? Framework.getService(
076                PageProviderService.class).getPageProviderDefinition(pageProviderName).getSearchDocumentType()
077                : null;
078
079        DocumentModel savedSearchDoc = session.createDocumentModel(uws.getPathAsString(), title,
080                searchDocumentType != null ? searchDocumentType
081                        : SavedSearchConstants.PARAMETERIZED_SAVED_SEARCH_TYPE_NAME);
082
083        SavedSearch savedSearch = savedSearchDoc.getAdapter(SavedSearch.class);
084        savedSearch.setTitle(title);
085        savedSearch.setQueryParams(queryParams);
086        savedSearch.setNamedParams(namedParams);
087        savedSearch.setQuery(query);
088        savedSearch.setQueryLanguage(queryLanguage);
089        savedSearch.setPageProviderName(pageProviderName);
090        savedSearch.setPageSize(pageSize);
091        savedSearch.setCurrentPageIndex(currentPageIndex);
092        savedSearch.setMaxResults(maxResults);
093        savedSearch.setSortBy(sortBy);
094        savedSearch.setSortOrder(sortOrder);
095        savedSearch.setContentViewData(contentViewData);
096
097        savedSearchDoc = session.createDocument(savedSearchDoc);
098        savedSearch = savedSearchDoc.getAdapter(SavedSearch.class);
099
100        ACP acp = savedSearchDoc.getACP();
101        ACL acl = acp.getOrCreateACL(ACL.LOCAL_ACL);
102        Principal principal = session.getPrincipal();
103        if (principal != null) {
104            acl.add(new ACE(principal.getName(), SecurityConstants.EVERYTHING, true));
105        }
106
107        acp.addACL(acl);
108        savedSearchDoc.setACP(acp, true);
109
110        return savedSearch;
111    }
112
113    @Override
114    public SavedSearch getSavedSearch(CoreSession session, String id) {
115        DocumentRef docRef = new IdRef(id);
116        DocumentModel savedSearchDoc = session.getDocument(docRef);
117        if (savedSearchDoc != null) {
118            return savedSearchDoc.getAdapter(SavedSearch.class);
119        }
120        return null;
121    }
122
123    @Override
124    public SavedSearch saveSavedSearch(CoreSession session, SavedSearch search) throws InvalidSearchParameterException,
125            IOException {
126        if (StringUtils.isEmpty(search.getTitle())) {
127            throw new InvalidSearchParameterException("title cannot be empty");
128        }
129
130        if ((!StringUtils.isEmpty(search.getQuery()) || !StringUtils.isEmpty(search.getQueryLanguage()))
131                && !StringUtils.isEmpty(search.getPageProviderName())) {
132            throw new InvalidSearchParameterException("query and page provider parameters are mutually exclusive"
133                    + " (query, queryLanguage, pageProviderName)");
134        }
135
136        if (StringUtils.isEmpty(search.getQuery()) && StringUtils.isEmpty(search.getQueryLanguage())
137                && StringUtils.isEmpty(search.getPageProviderName())) {
138            throw new InvalidSearchParameterException("query or page provider parameters are missing"
139                    + " (query, queryLanguage, pageProviderName)");
140        }
141
142        if (!StringUtils.isEmpty(search.getQuery()) && StringUtils.isEmpty(search.getQueryLanguage())) {
143            throw new InvalidSearchParameterException("queryLanguage parameter is missing");
144        }
145
146        if (StringUtils.isEmpty(search.getQuery()) && !StringUtils.isEmpty(search.getQueryLanguage())) {
147            throw new InvalidSearchParameterException("query parameter is missing");
148        }
149
150        DocumentModel doc = session.saveDocument(search.getDocument());
151        search.setDocument(doc);
152
153        return search;
154    }
155
156    @Override
157    public void deleteSavedSearch(CoreSession session, SavedSearch search) {
158        session.removeDocument(new IdRef(search.getId()));
159    }
160}