properties, String folderName, DocumentType documentType)
+ {
+ String recordName = getPropertyValue(properties, RMProperty.NAME);
+ String recordContent = getPropertyValue(properties, RMProperty.CONTENT);
+ return (getRecord(username, password, folderName, recordName) != null) || (contentService.createDocumentInFolder(username, password, RM_SITE_ID, folderName, documentType, recordName, recordContent) != null);
+ }
+
+ /**
+ * Delete a record from the given path
+ *
+ * @param username user's username
+ * @param password its password
+ * @param recordName the record name
+ * @param categoryName the name of the category in which the folder is, in case of unfiled record, this will have UNFILED_RECORDS_BREADCRUMB as container
+ * @param folderName folder name, in case in which trying to delete a record in Unfiled records directly, this will be ""
+ * @return true if the delete is successful
+ * eg. of usage in the case in which the record is inside a folder in Unfiled Records : deleteRecord(getAdminName(), getAdminPassword(), "f1 (2016-1472716888713)", UNFILED_RECORDS_BREADCRUMB, "unfiled records folder");
+ * eg. of usage in the case in which the record is created directly in Unfiled Records : deleteRecord(getAdminName(), getAdminPassword(), "f1 (2016-1472716888713)", UNFILED_RECORDS_BREADCRUMB, "");
+ */
+ public boolean deleteRecord(String username, String password, String recordName, String categoryName, String folderName)
+ {
+ String recordPath = "/" + categoryName;
+ if (!folderName.equals(""))
+ {
+ recordPath = recordPath + "/" + folderName;
+ }
+ return deleteItem(username, password, recordPath + "/" + recordName);
+ }
+
+ /**
+ * Retrieves the record object in case it exists
+ *
+ * @param username the user's username
+ * @param password its password
+ * @param folderName the folder in which the record is supposed to exist
+ * @param recordName the String with which the record name starts
+ * @return the record object in case it exists, null otherwise
+ */
+ private CmisObject getRecord(String username, String password, String folderName, String recordName)
+ {
+ for (CmisObject record : contentService.getFolderObject(contentService.getCMISSession(username, password), RM_SITE_ID, folderName).getChildren())
+ {
+ if (record.getName().startsWith(recordName))
+ {
+ return record;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Retrieves record full name for given partial name
+ *
+ * @param username the user's username
+ * @param password its password
+ * @param folderName the folder in which the record is supposed to exist
+ * @param recordPartialName the String with which the record name starts
+ * @return the record name in case it exists, empty String otherwise
+ */
+ public String getRecordFullName(String username, String password, String folderName, String recordPartialName)
+ {
+ CmisObject record = getRecord(username, password, folderName, recordPartialName);
+ if (record != null)
+ {
+ return record.getName();
+ }
+ return "";
+ }
+
+}
diff --git a/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/SearchAPI.java b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/SearchAPI.java
new file mode 100644
index 0000000000..7bcfff7d48
--- /dev/null
+++ b/rm-automation/rm-automation-community-rest-api/src/main/java/org/alfresco/rest/v0/SearchAPI.java
@@ -0,0 +1,177 @@
+/*
+ * #%L
+ * Alfresco Records Management Module
+ * %%
+ * Copyright (C) 2005 - 2017 Alfresco Software Limited
+ * %%
+ * This file is part of the Alfresco software.
+ * -
+ * If the software was purchased under a paid Alfresco license, the terms of
+ * the paid license agreement will prevail. Otherwise, the software is
+ * provided under the following open source license terms:
+ * -
+ * Alfresco is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * -
+ * Alfresco is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ * -
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with Alfresco. If not, see .
+ * #L%
+ */
+package org.alfresco.rest.v0;
+
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.alfresco.dataprep.AlfrescoHttpClientFactory;
+import org.alfresco.rest.core.v0.BaseAPI;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.utils.URLEncodedUtils;
+import org.apache.http.message.BasicNameValuePair;
+import org.json.JSONObject;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * Helper methods for performing search using various Alfresco search APIs.
+ *
+ * @author Kristijan Conkas
+ * @since 2.5
+ */
+@Component
+public class SearchAPI extends BaseAPI
+{
+ /** http client factory */
+ @Autowired private AlfrescoHttpClientFactory alfrescoHttpClientFactory;
+
+ /** faceted search API endpoint */
+ private static final String FACETED_SEARCH_ENDPOINT = "{0}alfresco/s/slingshot/rmsearch/faceted/rmsearch?{1}";
+
+ /** RM search URL template */
+ private static final String RM_SEARCH_ENDPOINT = "{0}alfresco/s/slingshot/rmsearch/{1}?{2}";
+
+ /** RM document search filters */
+ private static final String RM_DEFAULT_RECORD_FILTERS =
+ "records/true,undeclared/true,vital/false,folders/false,categories/false,frozen/false,cutoff/false";
+
+ /**
+ * Perform search request on search endpoint as a user.
+ *
+ * This method is applicable only to endpoints that support HTTP GET requests and return JSON body as response.
+ * @param searchEndpoint
+ * @param searchUser
+ * @param searchPassword
+ * @return search results as a {@link JSONObject}, please refer to API documentation for details
+ */
+ private JSONObject doSearch(
+ String searchEndpoint,
+ String searchUser,
+ String searchPassword)
+ {
+ return facetedRequest(searchUser, searchPassword, null, searchEndpoint);
+ }
+
+ /**
+ * Generic rm search.
+ * @param username
+ * @param password
+ * @param site
+ * @param query
+ * @param filters
+ * @return search results (see API reference for more details), null for any errors
+ */
+ public JSONObject rmSearch(
+ String username,
+ String password,
+ String site,
+ String query,
+ String filters)
+ {
+ List searchParameters = new ArrayList();
+ searchParameters.add(new BasicNameValuePair("query", query));
+ searchParameters.add(new BasicNameValuePair("filters", filters));
+
+ String requestURL = MessageFormat.format(
+ RM_SEARCH_ENDPOINT,
+ alfrescoHttpClientFactory.getObject().getAlfrescoUrl(),
+ (site != null) ? site : RM_SITE_ID,
+ URLEncodedUtils.format(searchParameters, "UTF-8"));
+
+ return doSearch(requestURL, username, password);
+ }
+
+ /**
+ * Search as a user for records on site "rm" matching query, using SearchAPI.RM_DEFAULT_RECORD_FILTERS
+ *
+ * If more fine-grained control of search parameters is required, use rmSearch() directly.
+ * @param username
+ * @param password
+ * @param query
+ * @return list of record names
+ */
+ public List searchForRecordsAsUser(
+ String username,
+ String password,
+ String query)
+ {
+ return getItemNames(rmSearch(username, password, "rm", query, RM_DEFAULT_RECORD_FILTERS));
+ }
+
+ /**
+ * Generic faceted search.
+ * @param username
+ * @param password
+ * @param parameters
+ * @return search results (see API reference for more details), null for any errors
+ */
+ public JSONObject facetedSearch(String username, String password, List parameters)
+ {
+ return facetedRequest(username, password, parameters, FACETED_SEARCH_ENDPOINT);
+ }
+
+ /**
+ * Execute faceted search for term.
+ * @param searchUser
+ * @param searchPassword
+ * @param searchTerm
+ * @return search results (see API reference for more details)
+ */
+ public JSONObject facetedSearchForTerm(String searchUser, String searchPassword, String searchTerm)
+ {
+ return facetedSearch(
+ searchUser,
+ searchPassword,
+ Arrays.asList(new BasicNameValuePair("term", searchTerm)));
+ }
+
+ /**
+ * Helper method to search for documents as a user using faceted search.
+ * @param username to search as
+ * @param password for username
+ * @param term search term
+ * @return list of document names found
+ */
+ public List searchForDocumentsAsUser(String username, String password, String term)
+ {
+ return getItemNames(facetedSearchForTerm(username, password, term));
+ }
+
+ /**
+ * Helper method to extract list of names from search result.
+ * @param searchResult
+ * @return list of document or record names in search result
+ * @throws RuntimeException for malformed search response
+ */
+ private List getItemNames(JSONObject searchResult)
+ {
+ return getPropertyValues(searchResult, "name");
+ }
+}