Java tutorial
/* * This work was created by participants in the DataONE project, and is * jointly copyrighted by participating institutions in DataONE. For * more information on DataONE, see our web site at http://dataone.org. * * Copyright 2014 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package org.dataone.proto.trove.mn.dao.v1; import org.dataone.proto.trove.mn.dao.v1.SolrLogIndexer; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Properties; import javax.inject.Named; import org.apache.commons.configuration.CompositeConfiguration; import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.log4j.Logger; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.UpdateResponse; import org.dataone.configuration.Settings; import org.dataone.proto.trove.mn.domain.v1.ObjectInfoSolrItem; import org.dataone.proto.trove.mn.service.v1.SolrIndex; import org.dataone.service.types.v1.ObjectInfo; import org.dataone.service.types.v1.ObjectList; /** * Currently, only concerned with a limited number of access fields * * @author waltz */ public class SolrQueryIndexer implements SolrIndex<ObjectInfo, Properties, ObjectList> { Logger logger = Logger.getLogger(SolrLogIndexer.class.getName()); int defaultCount = Settings.getConfiguration().getInt("mn.object.search.count.default"); private SolrServer solrServer; // Default indexing is to add all objects as originals @Override public Boolean add(ObjectInfo indexObjectInfo) { ObjectInfoSolrItem indexObjectInfoSolrItem = new ObjectInfoSolrItem(indexObjectInfo, Boolean.FALSE); Boolean rtn = Boolean.TRUE; try { UpdateResponse reponse = solrServer.addBean(indexObjectInfoSolrItem); if (reponse.getStatus() != 0) { rtn = Boolean.FALSE; } else { reponse = solrServer.commit(); if (reponse.getStatus() != 0) { rtn = Boolean.FALSE; } } } catch (SolrServerException ex) { // what to do with this exception??? looks bad. ex.printStackTrace(); } catch (IOException ex) { // what to do with this exception??? retry? ex.printStackTrace(); } return rtn; } @Override public Boolean add(List<ObjectInfo> indexObjectInfoEntries) { Boolean rtn = Boolean.TRUE; List<ObjectInfoSolrItem> indexObjectInfoSolrItemList = new ArrayList<>(indexObjectInfoEntries.size()); for (ObjectInfo objectInfoEntry : indexObjectInfoEntries) { indexObjectInfoSolrItemList.add(new ObjectInfoSolrItem(objectInfoEntry, Boolean.FALSE)); } try { UpdateResponse reponse = solrServer.addBeans(indexObjectInfoSolrItemList); if (reponse.getStatus() != 0) { rtn = Boolean.FALSE; } else { reponse = solrServer.commit(); if (reponse.getStatus() != 0) { rtn = Boolean.FALSE; } } } catch (SolrServerException ex) { // what to do with this exception??? looks bad. ex.printStackTrace(); } catch (IOException ex) { // what to do with this exception??? retry? ex.printStackTrace(); } return rtn; } @Override public ObjectList search(Properties query) throws Exception { // since this is the implementation assume the input to be a properties file. Configuration apacheConfiguration = getApacheConfiguration(query); ObjectList objectList = new ObjectList(); SolrQuery queryParams = new SolrQuery(); queryParams.setQuery(queryStringBuilder(apacheConfiguration)); queryParams.setSortField("id", SolrQuery.ORDER.desc); if (apacheConfiguration.containsKey("start")) { queryParams.setStart(apacheConfiguration.getInt("start")); } else { queryParams.setStart(0); } if (apacheConfiguration.containsKey("count")) { queryParams.setRows(apacheConfiguration.getInt("count")); } else { queryParams.setRows(defaultCount); } QueryResponse queryResponse = solrServer.query(queryParams); queryResponse.getResults(); List<ObjectInfoSolrItem> objectInfoList = queryResponse.getBeans(ObjectInfoSolrItem.class); for (ObjectInfoSolrItem lesi : objectInfoList) { objectList.addObjectInfo(lesi.getObjectInfo()); } objectList.setTotal(Long.valueOf(queryResponse.getResults().getNumFound()).intValue()); objectList.setStart(Long.valueOf(queryResponse.getResults().getStart()).intValue()); objectList.setCount(objectInfoList.size()); return objectList; } /** * Include additional properties in the global configuration. Properties included in the given file will override * existing properties in the global configuration if they are present. * * @param addProperties * @return Configuration * @throws ConfigurationException * @throws java.io.IOException */ public Configuration getApacheConfiguration(Properties addProperties) throws ConfigurationException, IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); addProperties.store(baos, "hold your comments please"); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); PropertiesConfiguration propertyConfiguration = new PropertiesConfiguration(); propertyConfiguration.load(bais); CompositeConfiguration compositeConfiguration = new CompositeConfiguration(); compositeConfiguration.addConfiguration(propertyConfiguration); return compositeConfiguration; } /** * Using the properties passed in the apache configuration file, build and return a valid Solr query string * * @param apacheConfiguration * @return String */ private String queryStringBuilder(Configuration apacheConfiguration) { // This should be refactored to use a query builder StringBuilder queryStringBuilder = new StringBuilder(); // first figure out the date parameters for the query if there is any if (apacheConfiguration.containsKey("fromDate")) { if (!apacheConfiguration.containsKey("toDate")) { queryStringBuilder.append( "dateSysMetadataModified:[" + apacheConfiguration.getString("fromDate") + " TO NOW]" + ""); } else { queryStringBuilder.append("dateSysMetadataModified:[" + apacheConfiguration.getString("fromDate") + " TO " + apacheConfiguration.getString("toDate") + "]"); } } else if (apacheConfiguration.containsKey("toDate")) { queryStringBuilder .append("dateSysMetadataModified:[* TO " + apacheConfiguration.getString("toDate") + "]"); } // add in the objectformat if (apacheConfiguration.containsKey("formatId")) { if (queryStringBuilder.length() > 0) { queryStringBuilder.append(" AND "); } queryStringBuilder.append("objectFormatId:\"" + apacheConfiguration.getString("formatId") + "\""); } // add in the replicaStatus if (apacheConfiguration.containsKey("replicaStatus")) { if (queryStringBuilder.length() > 0) { queryStringBuilder.append(" AND "); } queryStringBuilder.append("replicaStatus:" + apacheConfiguration.getString("replicaStatus")); } return queryStringBuilder.toString(); } }