Search in sources :

Example 11 with SearchService

use of org.alfresco.service.cmr.search.SearchService in project alfresco-repository by Alfresco.

the class ScriptSearchTest method mockServiceRegistry.

private ServiceRegistry mockServiceRegistry() {
    SearchService searchService = mock(SearchService.class);
    ResultSet results = mock(ResultSet.class);
    List<Pair<String, Integer>> fieldFacets1 = new ArrayList<>();
    fieldFacets1.add(new Pair<>(mimetype1, 1));
    fieldFacets1.add(new Pair<>(mimetype2, 2));
    List<Pair<String, Integer>> fieldFacets2 = new ArrayList<>();
    fieldFacets2.add(new Pair<>(modifier, 1));
    when(results.getFieldFacet(fieldFacet1)).thenReturn(fieldFacets1);
    when(results.getFieldFacet(fieldFacet2)).thenReturn(fieldFacets2);
    when(results.getFacetQueries()).thenReturn(new HashMap<>());
    when(searchService.query((SearchParameters) any())).thenReturn(results);
    FacetLabelDisplayHandlerRegistry displayHandlerRegistry = mock(FacetLabelDisplayHandlerRegistry.class);
    ServiceRegistry services = mock(ServiceRegistry.class);
    when(services.getSearchService()).thenReturn(searchService);
    when(displayHandlerRegistry.getDisplayHandler(fieldFacet1)).thenReturn(new MimetypeOrderDisplayHandler());
    when(displayHandlerRegistry.getDisplayHandler(fieldFacet2)).thenReturn(null);
    SolrFacetHelper solrFacetHelper = mock(SolrFacetHelper.class);
    when(solrFacetHelper.getBucketedFieldFacets()).thenReturn(new HashSet<>());
    when(services.getSolrFacetHelper()).thenReturn(solrFacetHelper);
    when(services.getFacetLabelDisplayHandlerRegistry()).thenReturn(displayHandlerRegistry);
    return services;
}
Also used : SearchService(org.alfresco.service.cmr.search.SearchService) ResultSet(org.alfresco.service.cmr.search.ResultSet) FacetLabelDisplayHandlerRegistry(org.alfresco.repo.search.impl.solr.facet.handler.FacetLabelDisplayHandlerRegistry) ServiceRegistry(org.alfresco.service.ServiceRegistry) Pair(org.alfresco.util.Pair) SolrFacetHelper(org.alfresco.repo.search.impl.solr.facet.SolrFacetHelper)

Example 12 with SearchService

use of org.alfresco.service.cmr.search.SearchService in project acs-community-packaging by Alfresco.

the class ContextListener method contextInitialized.

/**
 * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
 */
public void contextInitialized(ServletContextEvent event) {
    // make sure that the spaces store in the repository exists
    this.servletContext = event.getServletContext();
    WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    // If no context has been initialised, exit silently so config changes can be made
    if (ctx == null) {
        return;
    }
    ServiceRegistry registry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
    TransactionService transactionService = registry.getTransactionService();
    NodeService nodeService = registry.getNodeService();
    SearchService searchService = registry.getSearchService();
    NamespaceService namespaceService = registry.getNamespaceService();
    AuthenticationContext authenticationContext = (AuthenticationContext) ctx.getBean("authenticationContext");
    // repo bootstrap code for our client
    UserTransaction tx = null;
    NodeRef companySpaceNodeRef = null;
    try {
        tx = transactionService.getUserTransaction();
        tx.begin();
        authenticationContext.setSystemUserAsCurrentUser();
        // get and setup the initial store ref and root path from config
        StoreRef storeRef = Repository.getStoreRef(servletContext);
        // get root path
        String rootPath = Application.getRootPath(servletContext);
        // Extract company space id and store it in the Application object
        companySpaceNodeRef = Repository.getCompanyRoot(nodeService, searchService, namespaceService, storeRef, rootPath);
        Application.setCompanyRootId(companySpaceNodeRef.getId());
        // commit the transaction
        tx.commit();
    } catch (Throwable e) {
        // rollback the transaction
        try {
            if (tx != null) {
                tx.rollback();
            }
        } catch (Exception ex) {
        }
        logger.error("Failed to initialise ", e);
        throw new AlfrescoRuntimeException("Failed to initialise ", e);
    } finally {
        try {
            authenticationContext.clearCurrentSecurityContext();
        } catch (Exception ex) {
        }
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) StoreRef(org.alfresco.service.cmr.repository.StoreRef) AuthenticationContext(org.alfresco.repo.security.authentication.AuthenticationContext) TransactionService(org.alfresco.service.transaction.TransactionService) NodeService(org.alfresco.service.cmr.repository.NodeService) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) WebApplicationContext(org.springframework.web.context.WebApplicationContext) NodeRef(org.alfresco.service.cmr.repository.NodeRef) NamespaceService(org.alfresco.service.namespace.NamespaceService) SearchService(org.alfresco.service.cmr.search.SearchService) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ServiceRegistry(org.alfresco.service.ServiceRegistry)

Example 13 with SearchService

use of org.alfresco.service.cmr.search.SearchService in project acs-community-packaging by Alfresco.

the class User method getUserPreferencesRef.

/**
 * Get or create the node used to store user preferences.
 * Utilises the 'configurable' aspect on the Person linked to this user.
 */
synchronized NodeRef getUserPreferencesRef(WebApplicationContext context) {
    final ServiceRegistry registry = (ServiceRegistry) context.getBean("ServiceRegistry");
    final NodeService nodeService = registry.getNodeService();
    final SearchService searchService = registry.getSearchService();
    final NamespaceService namespaceService = registry.getNamespaceService();
    final TransactionService txService = registry.getTransactionService();
    final ConfigurableService configurableService = (ConfigurableService) context.getBean("ConfigurableService");
    RetryingTransactionHelper txnHelper = registry.getTransactionService().getRetryingTransactionHelper();
    return txnHelper.doInTransaction(new RetryingTransactionCallback<NodeRef>() {

        public NodeRef execute() throws Throwable {
            NodeRef prefRef = null;
            NodeRef person = getPerson();
            if (nodeService.hasAspect(person, ApplicationModel.ASPECT_CONFIGURABLE) == false) {
                // if the repository is in read-only mode just return null
                if (txService.isReadOnly()) {
                    return null;
                } else {
                    // create the configuration folder for this Person node
                    configurableService.makeConfigurable(person);
                }
            }
            // target of the assoc is the configurations folder ref
            NodeRef configRef = configurableService.getConfigurationFolder(person);
            if (configRef == null) {
                throw new IllegalStateException("Unable to find associated 'configurations' folder for node: " + person);
            }
            String xpath = NamespaceService.APP_MODEL_PREFIX + ":" + "preferences";
            List<NodeRef> nodes = searchService.selectNodes(configRef, xpath, null, namespaceService, false);
            if (nodes.size() == 1) {
                prefRef = nodes.get(0);
            } else {
                // create the preferences Node for this user (if repo is not read-only)
                if (txService.isReadOnly() == false) {
                    ChildAssociationRef childRef = nodeService.createNode(configRef, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.APP_MODEL_1_0_URI, "preferences"), ContentModel.TYPE_CMOBJECT);
                    prefRef = childRef.getChildRef();
                }
            }
            return prefRef;
        }
    }, txService.isReadOnly());
}
Also used : TransactionService(org.alfresco.service.transaction.TransactionService) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) NodeService(org.alfresco.service.cmr.repository.NodeService) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) NodeRef(org.alfresco.service.cmr.repository.NodeRef) NamespaceService(org.alfresco.service.namespace.NamespaceService) SearchService(org.alfresco.service.cmr.search.SearchService) List(java.util.List) ServiceRegistry(org.alfresco.service.ServiceRegistry) ConfigurableService(org.alfresco.repo.configuration.ConfigurableService)

Example 14 with SearchService

use of org.alfresco.service.cmr.search.SearchService in project alfresco-remote-api by Alfresco.

the class SharedLinkApiTest method testSharedLinkFindIncludeAspects.

@Test
public void testSharedLinkFindIncludeAspects() throws Exception {
    PublicApiClient.Favourites favouritesProxy = publicApiClient.favourites();
    QuickShareLinksImpl quickShareLinks = applicationContext.getBean("quickShareLinks", QuickShareLinksImpl.class);
    ServiceDescriptorRegistry serviceRegistry = applicationContext.getBean("ServiceRegistry", ServiceDescriptorRegistry.class);
    SearchService mockSearchService = mock(SearchService.class);
    serviceRegistry.setMockSearchService(mockSearchService);
    // As user 1 ...
    setRequestContext(user1);
    Paging paging = getPaging(0, 100);
    String content = "The quick brown fox jumps over the lazy dog.";
    String fileName1 = "fileOne_" + RUNID + ".txt";
    String fileName2 = "fileTwo_" + RUNID + ".txt";
    // As user 1 create 2 text files in -my- folder (i.e. User's Home)
    setRequestContext(user1);
    String file1Id = createTextFile(getMyNodeId(), fileName1, content).getId();
    String file2Id = createTextFile(getMyNodeId(), fileName2, content).getId();
    // Create shared links to file 1 and 2
    QuickShareLink body = new QuickShareLink();
    body.setNodeId(file1Id);
    post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 201);
    body = new QuickShareLink();
    body.setNodeId(file2Id);
    post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 201);
    // Favourite file with file1Id file as user 1
    Favourite file1Favourite = makeFileFavourite(file1Id);
    favouritesProxy.createFavourite(user1, file1Favourite, null);
    // Find shared links without include=isFavorite
    ResultSet mockResultSet = mockResultSet(Arrays.asList(file1Id, file2Id));
    when(mockSearchService.query(any())).thenReturn(mockResultSet);
    quickShareLinks.afterPropertiesSet();
    HttpResponse response = getAll(URL_SHARED_LINKS, paging, null, 200);
    List<QuickShareLink> sharedLinks = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), QuickShareLink.class);
    assertEquals(2, sharedLinks.size());
    QuickShareLink resQuickShareLink1 = sharedLinks.get(0);
    QuickShareLink resQuickShareLink2 = sharedLinks.get(1);
    assertNull("aspectNames was not requested therefore it should not be included", resQuickShareLink1.getAspectNames());
    assertNull("aspectNames was not requested therefore it should not be included", resQuickShareLink2.getAspectNames());
    // Find shared links with include=isFavorite
    mockResultSet = mockResultSet(Arrays.asList(file1Id, file2Id));
    when(mockSearchService.query(any())).thenReturn(mockResultSet);
    quickShareLinks.afterPropertiesSet();
    Map<String, String> params = new HashMap<>();
    params.put("include", "aspectNames");
    response = getAll(URL_SHARED_LINKS, paging, params, 200);
    sharedLinks = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), QuickShareLink.class);
    assertEquals(2, sharedLinks.size());
    resQuickShareLink1 = sharedLinks.get(0);
    resQuickShareLink2 = sharedLinks.get(1);
    assertNotNull("aspectNames was not requested therefore it should not be included", resQuickShareLink1.getAspectNames());
    assertNotNull("aspectNames was not requested therefore it should not be included", resQuickShareLink2.getAspectNames());
    serviceRegistry.setMockSearchService(null);
    quickShareLinks.afterPropertiesSet();
}
Also used : HashMap(java.util.HashMap) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) QuickShareLinksImpl(org.alfresco.rest.api.impl.QuickShareLinksImpl) ServiceDescriptorRegistry(org.alfresco.repo.service.ServiceDescriptorRegistry) SearchService(org.alfresco.service.cmr.search.SearchService) ResultSet(org.alfresco.service.cmr.search.ResultSet) PublicApiClient(org.alfresco.rest.api.tests.client.PublicApiClient) QuickShareLink(org.alfresco.rest.api.model.QuickShareLink) Favourite(org.alfresco.rest.api.tests.client.data.Favourite) Test(org.junit.Test)

Example 15 with SearchService

use of org.alfresco.service.cmr.search.SearchService in project alfresco-remote-api by Alfresco.

the class SharedLinkApiTest method testSharedLinkFindIncludeIsFavorite.

@Test
public void testSharedLinkFindIncludeIsFavorite() throws Exception {
    PublicApiClient.Favourites favouritesProxy = publicApiClient.favourites();
    QuickShareLinksImpl quickShareLinks = applicationContext.getBean("quickShareLinks", QuickShareLinksImpl.class);
    ServiceDescriptorRegistry serviceRegistry = applicationContext.getBean("ServiceRegistry", ServiceDescriptorRegistry.class);
    SearchService mockSearchService = mock(SearchService.class);
    serviceRegistry.setMockSearchService(mockSearchService);
    // As user 1 ...
    setRequestContext(user1);
    Paging paging = getPaging(0, 100);
    String content = "The quick brown fox jumps over the lazy dog.";
    String fileName1 = "fileOne_" + RUNID + ".txt";
    String fileName2 = "fileTwo_" + RUNID + ".txt";
    // As user 1 create 2 text files in -my- folder (i.e. User's Home)
    setRequestContext(user1);
    String file1Id = createTextFile(getMyNodeId(), fileName1, content).getId();
    String file2Id = createTextFile(getMyNodeId(), fileName2, content).getId();
    // Create shared links to file 1 and 2
    QuickShareLink body = new QuickShareLink();
    body.setNodeId(file1Id);
    post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 201);
    body = new QuickShareLink();
    body.setNodeId(file2Id);
    post(URL_SHARED_LINKS, toJsonAsStringNonNull(body), 201);
    // Favourite file with file1Id file as user 1
    Favourite file1Favourite = makeFileFavourite(file1Id);
    favouritesProxy.createFavourite(user1, file1Favourite, null);
    // Find shared links without include=isFavorite
    ResultSet mockResultSet = mockResultSet(Arrays.asList(file1Id, file2Id));
    when(mockSearchService.query(any())).thenReturn(mockResultSet);
    quickShareLinks.afterPropertiesSet();
    HttpResponse response = response = getAll(URL_SHARED_LINKS, paging, null, 200);
    List<QuickShareLink> sharedLinks = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), QuickShareLink.class);
    assertEquals(2, sharedLinks.size());
    QuickShareLink resQuickShareLink1 = sharedLinks.get(0);
    QuickShareLink resQuickShareLink2 = sharedLinks.get(1);
    assertNull("isFavorite was not requested therefore it should not be included", resQuickShareLink1.getIsFavorite());
    assertNull("isFavorite was not requested therefore it should not be included", resQuickShareLink2.getIsFavorite());
    // Find shared links with include=isFavorite
    mockResultSet = mockResultSet(Arrays.asList(file1Id, file2Id));
    when(mockSearchService.query(any())).thenReturn(mockResultSet);
    quickShareLinks.afterPropertiesSet();
    Map<String, String> params = new HashMap<>();
    params.put("include", "isFavorite");
    response = getAll(URL_SHARED_LINKS, paging, params, 200);
    sharedLinks = RestApiUtil.parseRestApiEntries(response.getJsonResponse(), QuickShareLink.class);
    assertEquals(2, sharedLinks.size());
    resQuickShareLink1 = sharedLinks.get(0);
    resQuickShareLink2 = sharedLinks.get(1);
    assertTrue("Document should be marked as favorite.", resQuickShareLink1.getIsFavorite());
    assertFalse("Document should not be marked as favorite.", resQuickShareLink2.getIsFavorite());
    serviceRegistry.setMockSearchService(null);
    quickShareLinks.afterPropertiesSet();
}
Also used : HashMap(java.util.HashMap) Paging(org.alfresco.rest.api.tests.client.PublicApiClient.Paging) HttpResponse(org.alfresco.rest.api.tests.client.HttpResponse) QuickShareLinksImpl(org.alfresco.rest.api.impl.QuickShareLinksImpl) ServiceDescriptorRegistry(org.alfresco.repo.service.ServiceDescriptorRegistry) SearchService(org.alfresco.service.cmr.search.SearchService) ResultSet(org.alfresco.service.cmr.search.ResultSet) PublicApiClient(org.alfresco.rest.api.tests.client.PublicApiClient) QuickShareLink(org.alfresco.rest.api.model.QuickShareLink) Favourite(org.alfresco.rest.api.tests.client.data.Favourite) Test(org.junit.Test)

Aggregations

SearchService (org.alfresco.service.cmr.search.SearchService)27 NodeRef (org.alfresco.service.cmr.repository.NodeRef)14 NodeService (org.alfresco.service.cmr.repository.NodeService)14 ResultSet (org.alfresco.service.cmr.search.ResultSet)13 FileFolderService (org.alfresco.service.cmr.model.FileFolderService)9 TransactionService (org.alfresco.service.transaction.TransactionService)9 HashMap (java.util.HashMap)8 StoreRef (org.alfresco.service.cmr.repository.StoreRef)8 NamespaceService (org.alfresco.service.namespace.NamespaceService)8 ServiceRegistry (org.alfresco.service.ServiceRegistry)7 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)6 PersonService (org.alfresco.service.cmr.security.PersonService)6 QName (org.alfresco.service.namespace.QName)5 Before (org.junit.Before)5 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)4 Serializable (java.io.Serializable)3 ServiceDescriptorRegistry (org.alfresco.repo.service.ServiceDescriptorRegistry)3 QuickShareLinksImpl (org.alfresco.rest.api.impl.QuickShareLinksImpl)3 MutableAuthenticationService (org.alfresco.service.cmr.security.MutableAuthenticationService)3 IOException (java.io.IOException)2