/** @return Returns true if the pattern is present, otherwise false. */ public boolean like( NodeRef nodeRef, QName propertyQName, String sqlLikePattern, boolean includeFTS) { if (propertyQName == null) { throw new IllegalArgumentException("Property QName is mandatory for the like expression"); } StringBuilder sb = new StringBuilder(sqlLikePattern.length() * 3); if (includeFTS) { // convert the SQL-like pattern into a Lucene-compatible string String pattern = SearchLanguageConversion.convertXPathLikeToLucene(sqlLikePattern.toLowerCase()); // build Lucene search string specific to the node sb = new StringBuilder(); sb.append("+ID:\"").append(nodeRef.toString()).append("\" +("); // FTS or attribute matches if (includeFTS) { sb.append("TEXT:(").append(pattern).append(") "); } if (propertyQName != null) { sb.append(" @") .append( SearchLanguageConversion.escapeLuceneQuery( QName.createQName( propertyQName.getNamespaceURI(), ISO9075.encode(propertyQName.getLocalName())) .toString())) .append(":(") .append(pattern) .append(")"); } sb.append(")"); ResultSet resultSet = null; try { resultSet = this.query(nodeRef.getStoreRef(), "lucene", sb.toString()); boolean answer = resultSet.length() > 0; return answer; } finally { if (resultSet != null) { resultSet.close(); } } } else { // convert the SQL-like pattern into a Lucene-compatible string String pattern = SearchLanguageConversion.convertXPathLikeToRegex(sqlLikePattern.toLowerCase()); Serializable property = nodeService.getProperty(nodeRef, propertyQName); if (property == null) { return false; } else { String propertyString = DefaultTypeConverter.INSTANCE.convert( String.class, nodeService.getProperty(nodeRef, propertyQName)); return propertyString.toLowerCase().matches(pattern); } } }
/** * Retrieves the NodeRef of the deploymentattempt node with the given id * * @param attemptId The deployattemptid of the node to be found * @return The NodeRef of the deploymentattempt node or null if not found */ public static NodeRef findDeploymentAttempt(String attemptId) { FacesContext fc = FacesContext.getCurrentInstance(); SearchService searchService = Repository.getServiceRegistry(fc).getSearchService(); // construct the query StringBuilder query = new StringBuilder("@"); query.append(NamespaceService.WCMAPP_MODEL_PREFIX); query.append("\\:"); query.append(WCMAppModel.PROP_DEPLOYATTEMPTID.getLocalName()); query.append(":\""); query.append(attemptId); query.append("\""); ResultSet results = null; NodeRef attempt = null; try { // execute the query results = searchService.query( Repository.getStoreRef(), SearchService.LANGUAGE_LUCENE, query.toString()); if (results.length() == 1) { attempt = results.getNodeRef(0); } else if (results.length() > 1) { throw new IllegalStateException( "More than one deployment attempt node was found, there should only be one!"); } } finally { if (results != null) { results.close(); } } return attempt; }
/** * @see * org.alfresco.service.cmr.coci.CheckOutCheckInService#getWorkingCopy(org.alfresco.service.cmr.repository.NodeRef) */ public NodeRef getWorkingCopy(NodeRef nodeRef) { NodeRef workingCopy = null; // Do a search to find the working copy document ResultSet resultSet = null; try { resultSet = this.searchService.query( nodeRef.getStoreRef(), SearchService.LANGUAGE_LUCENE, "+ASPECT:\"" + ContentModel.ASPECT_WORKING_COPY.toString() + "\" +@\\{http\\://www.alfresco.org/model/content/1.0\\}" + ContentModel.PROP_COPY_REFERENCE.getLocalName() + ":\"" + nodeRef.toString() + "\""); if (resultSet.getNodeRefs().size() != 0) { workingCopy = resultSet.getNodeRef(0); } } finally { if (resultSet != null) { resultSet.close(); } } return workingCopy; }
public static void closeQuietly(ResultSet resultSet) { try { resultSet.close(); } catch (Exception ex) { // just swallow... } }
/** @return Returns true if the pattern is present, otherwise false. */ public boolean contains( NodeRef nodeRef, QName propertyQName, String googleLikePattern, SearchParameters.Operator defaultOperator) { ResultSet resultSet = null; try { // build Lucene search string specific to the node StringBuilder sb = new StringBuilder(); sb.append("+ID:\"") .append(nodeRef.toString()) .append("\" +(TEXT:(") .append(googleLikePattern.toLowerCase()) .append(") "); if (propertyQName != null) { sb.append(" OR @") .append( SearchLanguageConversion.escapeLuceneQuery( QName.createQName( propertyQName.getNamespaceURI(), ISO9075.encode(propertyQName.getLocalName())) .toString())); sb.append(":(").append(googleLikePattern.toLowerCase()).append(")"); } else { for (QName key : nodeService.getProperties(nodeRef).keySet()) { sb.append(" OR @") .append( SearchLanguageConversion.escapeLuceneQuery( QName.createQName(key.getNamespaceURI(), ISO9075.encode(key.getLocalName())) .toString())); sb.append(":(").append(googleLikePattern.toLowerCase()).append(")"); } } sb.append(")"); SearchParameters sp = new SearchParameters(); sp.setLanguage(SearchService.LANGUAGE_LUCENE); sp.setQuery(sb.toString()); sp.setDefaultOperator(defaultOperator); sp.addStore(nodeRef.getStoreRef()); resultSet = this.query(sp); boolean answer = resultSet.length() > 0; return answer; } finally { if (resultSet != null) { resultSet.close(); } } }
protected NodeRef getTemplateNode(RenderingContext context) { NodeRef node = context.getCheckedParam(PARAM_TEMPLATE_NODE, NodeRef.class); if (node == null) { String path = context.getCheckedParam(PARAM_TEMPLATE_PATH, String.class); if (path != null && path.length() > 0) { StoreRef storeRef = context.getDestinationNode().getStoreRef(); ResultSet result = null; try { result = searchService.query(storeRef, SearchService.LANGUAGE_XPATH, path); if (result.length() != 1) { throw new RenditionServiceException("Could not find template node for path: " + path); } node = result.getNodeRef(0); } finally { if (result != null) { result.close(); } } } } return node; }
/** * Returns the test servers allocated to the given store. * * @param store The store to get the test server for * @return The allocated server(s), an empty list if there isn't one */ public static List<NodeRef> findAllocatedTestServers(String store) { List<NodeRef> serverList = new ArrayList<NodeRef>(); FacesContext fc = FacesContext.getCurrentInstance(); SearchService searchService = Repository.getServiceRegistry(fc).getSearchService(); // construct the query StringBuilder query = new StringBuilder("@"); query.append(NamespaceService.WCMAPP_MODEL_PREFIX); query.append("\\:"); query.append(WCMAppModel.PROP_DEPLOYSERVERALLOCATEDTO.getLocalName()); query.append(":\""); query.append(store); query.append("\""); ResultSet results = null; NodeRef testServer = null; try { // execute the query results = searchService.query( Repository.getStoreRef(), SearchService.LANGUAGE_LUCENE, query.toString()); if (results.length() > 0) { for (int i = 0; i < results.length(); i++) { serverList.add(results.getNodeRef(i)); } } } finally { if (results != null) { results.close(); } } return serverList; }
/** * Get the value of the counter. * * @param parameters * @return * @throws Exception */ private String getCounter(Map<String, Object> parameters) throws Exception { String counter = "0"; switch ((String) parameters.get(PARAMETER_COUNTER)) { case PARAMETER_COUNTER_AUDIT_TRAIL: case PARAMETER_COUNTER_WORKFLOW_INSTANCES: case PARAMETER_COUNTER_WORKFLOW_TASKS: /* * TODO: The query should be defined using hibernate. */ Class.forName(properties.getProperty(DB_DRIVER)); java.sql.Connection dbConnection = DriverManager.getConnection( properties.getProperty(DB_URL), properties.getProperty(DB_USERNAME), properties.getProperty(DB_PASSWORD)); Statement dbStatement = dbConnection.createStatement(); String dbQuery = getDatabaseQuery(parameters); java.sql.ResultSet dbResultSet = dbStatement.executeQuery(dbQuery); if (dbResultSet.next()) { counter = String.valueOf(dbResultSet.getDouble(VALUE_FIELD)); } if (dbResultSet.next()) { throw new Exception( "Database query '" + dbQuery + "' with multiple results when only one is expected."); } if (dbStatement != null) dbStatement.close(); if (dbConnection != null) dbConnection.close(); break; case PARAMETER_COUNTER_ASPECTS: case PARAMETER_COUNTER_TYPES: SearchParameters searchParameters = new SearchParameters(); searchParameters.addStore(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE); searchParameters.setLanguage(SearchService.LANGUAGE_FTS_ALFRESCO); searchParameters.setPermissionEvaluation(PermissionEvaluationMode.NONE); searchParameters.setUseInMemorySort(false); searchParameters.setLimitBy(LimitBy.FINAL_SIZE); List<String> alfrescoQueries = getAlfrescoQueries(parameters); int sum = 0; for (int i = 0; i < alfrescoQueries.size(); ++i) { searchParameters.setQuery(alfrescoQueries.get(i)); org.alfresco.service.cmr.search.ResultSet resultSet = searchService.query(searchParameters); sum += resultSet.getNumberFound(); resultSet.close(); } counter = String.valueOf(sum); break; default: throw new Exception( "Parameter '" + PARAMETER_COUNTER + "' with value '" + ((String) parameters.get(PARAMETER_COUNTER)) + "' not admitted."); } return counter; }
/** * Returns all deployment attempts for the given store * * @param store The store to get the deployment attempts for * @param fromDate If present only attempts after the given date are returned * @param toDate If present only attempts before the given date are returned, if null toDate * defaults to today's date * @return List of NodeRef's representing the deployment attempts */ public static List<NodeRef> findDeploymentAttempts(String store, Date fromDate, Date toDate) { FacesContext fc = FacesContext.getCurrentInstance(); SearchService searchService = Repository.getServiceRegistry(fc).getSearchService(); // query for all deploymentattempt nodes with the deploymentattemptstore // set to the given store id StringBuilder query = new StringBuilder("@"); query.append(NamespaceService.WCMAPP_MODEL_PREFIX); query.append("\\:"); query.append(WCMAppModel.PROP_DEPLOYATTEMPTSTORE.getLocalName()); query.append(":\""); query.append(store); query.append("\""); // constrain the search by date if a fromDate is applied if (fromDate != null) { if (toDate == null) { toDate = new Date(); } // see if the dates are the same (ignoring the time) boolean sameDate = false; Calendar fromCal = new GregorianCalendar(); fromCal.setTime(fromDate); Calendar toCal = new GregorianCalendar(); toCal.setTime(toDate); if ((fromCal.get(Calendar.YEAR) == toCal.get(Calendar.YEAR)) && (fromCal.get(Calendar.MONTH) == toCal.get(Calendar.MONTH)) && (fromCal.get(Calendar.DAY_OF_MONTH) == toCal.get(Calendar.DAY_OF_MONTH))) { sameDate = true; } // add date to query query.append(" AND @"); query.append(NamespaceService.WCMAPP_MODEL_PREFIX); query.append("\\:"); query.append(WCMAppModel.PROP_DEPLOYATTEMPTTIME.getLocalName()); query.append(":"); if (sameDate) { // convert date into format needed for range query String queryDate = formatLuceneQueryDate(fromDate, false); // query for exact date query.append("\""); query.append(queryDate); query.append("\""); } else { // convert to date into format needed for range query String queryFromDate = formatLuceneQueryDate(fromDate, true); String queryToDate = formatLuceneQueryDate(toDate, true); // create a date range query query.append("["); query.append(queryFromDate); query.append(" TO "); query.append(queryToDate); query.append("]"); } } if (logger.isDebugEnabled()) logger.debug("Finding deploymentattempt nodes using query: " + query.toString()); ResultSet results = null; List<NodeRef> attempts = new ArrayList<NodeRef>(); try { // sort the results by deploymentattempttime SearchParameters sp = new SearchParameters(); sp.addStore(Repository.getStoreRef()); sp.setLanguage(SearchService.LANGUAGE_LUCENE); sp.setQuery(query.toString()); sp.addSort("@" + WCMAppModel.PROP_DEPLOYATTEMPTTIME, false); // execute the query results = searchService.query(sp); if (logger.isDebugEnabled()) logger.debug("Found " + results.length() + " deployment attempts"); for (NodeRef attempt : results.getNodeRefs()) { attempts.add(attempt); } } finally { if (results != null) { results.close(); } } return attempts; }
/** * Returns a list of NodeRefs representing the deployment servers configured for the given web * project. * * @param webProject Web project to get test servers for * @param live * @param availableOnly if true only returns those servers still available for deployment * @return List of test servers */ private static List<NodeRef> findServers( NodeRef webProject, boolean live, boolean availableOnly) { FacesContext context = FacesContext.getCurrentInstance(); NodeService nodeService = Repository.getServiceRegistry(context).getNodeService(); SearchService searchService = Repository.getServiceRegistry(context).getSearchService(); NamespacePrefixResolver namespacePrefixResolver = Repository.getServiceRegistry(context).getNamespaceService(); Path projectPath = nodeService.getPath(webProject); String stringPath = projectPath.toPrefixString(namespacePrefixResolver); StringBuilder query = new StringBuilder("PATH:\""); query.append(stringPath); query.append("/*\" "); query.append(" AND @"); query.append(NamespaceService.WCMAPP_MODEL_PREFIX); query.append("\\:"); query.append(WCMAppModel.PROP_DEPLOYSERVERTYPE.getLocalName()); query.append(":\""); if (live) { query.append(WCMAppModel.CONSTRAINT_LIVESERVER); } else { query.append(WCMAppModel.CONSTRAINT_TESTSERVER); } query.append("\""); // if required filter the test servers if (live == false && availableOnly) { query.append(" AND ISNULL:\""); query.append(WCMAppModel.PROP_DEPLOYSERVERALLOCATEDTO.toString()); query.append("\""); } if (logger.isDebugEnabled()) logger.debug("Finding deployment servers using query: " + query.toString()); // execute the query ResultSet results = null; List<NodeRef> servers = new ArrayList<NodeRef>(); try { results = searchService.query( Repository.getStoreRef(), SearchService.LANGUAGE_LUCENE, query.toString()); if (logger.isDebugEnabled()) logger.debug("Found " + results.length() + " deployment servers"); for (NodeRef server : results.getNodeRefs()) { servers.add(server); } } finally { if (results != null) { results.close(); } } return servers; }