Skip to content

Commit

Permalink
Small refactorings of loops in fcrepo-kernel and moving literal strin…
Browse files Browse the repository at this point in the history
…gs into constants
  • Loading branch information
ajs6f committed Mar 22, 2013
1 parent d2f7092 commit 63162f2
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 92 deletions.
171 changes: 87 additions & 84 deletions fcrepo-http-api/src/main/java/org/fcrepo/api/FedoraFieldSearch.java
@@ -1,10 +1,11 @@

package org.fcrepo.api;

import static com.google.common.collect.ImmutableList.builder;
import static java.lang.Integer.parseInt;
import static javax.jcr.query.Query.JCR_SQL2;
import static javax.ws.rs.core.MediaType.TEXT_HTML;
import static javax.ws.rs.core.Response.ok;

import java.util.ArrayList;
import java.util.List;
import static org.slf4j.LoggerFactory.getLogger;

import javax.jcr.LoginException;
import javax.jcr.Node;
Expand All @@ -19,69 +20,69 @@
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

import org.fcrepo.AbstractResource;
import org.fcrepo.jaxb.search.FieldSearchResult;
import org.fcrepo.jaxb.search.ObjectFields;
import org.fcrepo.provider.VelocityViewer;
import org.fcrepo.utils.FedoraJcrTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.ImmutableList;

/**
* @author Vincent Nguyen
*/

@Path("/search")
public class FedoraFieldSearch extends AbstractResource {
private static final Logger logger = LoggerFactory
.getLogger(FedoraFieldSearch.class);
public class FedoraFieldSearch extends AbstractResource implements
FedoraJcrTypes {

private static final Logger logger = getLogger(FedoraFieldSearch.class);

private QueryResult queryResults = null;

private QueryResult queryResults = null;

@GET
@GET
@Produces(TEXT_HTML)
public Response searchForm() throws LoginException,
RepositoryException {

VelocityViewer view = new VelocityViewer();
return ok(view.getFieldSearch(null)).build();
public String searchForm() throws LoginException, RepositoryException {
return new VelocityViewer().getFieldSearch(null);
}

@POST
@Produces(TEXT_HTML)
public Response searchSubmit(@FormParam("terms") String terms,
@FormParam("offSet") String offSet,
@FormParam("maxResults") String maxResults) throws LoginException,
RepositoryException {

final Session session = repo.login();
QueryManager queryManager = session.getWorkspace().getQueryManager();
VelocityViewer view = new VelocityViewer();

logger.debug("Searching for " + terms);

//TODO expand to more fields
String sqlExpression = "SELECT * FROM [fedora:object] WHERE";
sqlExpression += " [dc:identifier] like $sterm";
sqlExpression += " OR [dc:title] like $sterm";
String language = Query.JCR_SQL2;
Query query = queryManager.createQuery(sqlExpression,language);
query.bindValue("sterm", session.getValueFactory().createValue("%" + terms + "%"));
logger.debug("statement is " + query.getStatement());

if (offSet == null) {
offSet = "0";
}

FieldSearchResult fsr = search(query, Integer.parseInt(offSet), Integer.parseInt(maxResults));
fsr.setSearchTerms(terms);

session.logout();

return ok(view.getFieldSearch(fsr)).build();
public String searchSubmit(@FormParam("terms")
String terms, @FormParam("offSet")
String offSet, @FormParam("maxResults")
String maxResults) throws LoginException, RepositoryException {

final Session session = repo.login();
QueryManager queryManager = session.getWorkspace().getQueryManager();
VelocityViewer view = new VelocityViewer();

logger.debug("Searching for " + terms);

//TODO expand to more fields
String sqlExpression = "SELECT * FROM [" + FEDORA_OBJECT + "] WHERE";
sqlExpression += " [" + DC_IDENTIFIER + "] like $sterm";
sqlExpression += " OR [" + DC_TITLE + "] like $sterm";
Query query = queryManager.createQuery(sqlExpression, JCR_SQL2);
query.bindValue("sterm", session.getValueFactory().createValue(
"%" + terms + "%"));
logger.debug("statement is " + query.getStatement());

if (offSet == null) {
offSet = "0";
}

FieldSearchResult fsr =
search(query, parseInt(offSet), parseInt(maxResults));
fsr.setSearchTerms(terms);

session.logout();

return view.getFieldSearch(fsr);
}

/**
* Searches the repository using JCR SQL2 queries and returns a FieldSearchResult object
* @param sqlExpression
Expand All @@ -91,40 +92,42 @@ public Response searchSubmit(@FormParam("terms") String terms,
* @throws LoginException
* @throws RepositoryException
*/
public FieldSearchResult search(Query query, int offSet, int maxResults) throws LoginException,
RepositoryException {

List<ObjectFields> fieldObjects = new ArrayList<ObjectFields>();

//if offSet is 0, we assume it's the first query and set the queryResults
if (offSet == 0) {
queryResults = query.execute();
}

NodeIterator nodeIter = queryResults.getNodes();
int size = (int)nodeIter.getSize();
logger.debug(size + " results found");

//add the next set of results to the fieldObjects starting at offSet for pagination
int i=offSet;
nodeIter.skip((long)offSet);
while ( nodeIter.hasNext() && i < (offSet + maxResults)) {
ObjectFields obj = new ObjectFields();
try {
Node node = nodeIter.nextNode();
obj.setPid(node.getName());
obj.setPath(node.getPath());
fieldObjects.add(obj);
} catch (RepositoryException ex) {
logger.error(ex.getMessage());
}
i++;
}

FieldSearchResult fsr = new FieldSearchResult(fieldObjects, offSet, maxResults, size);
fsr.setStart(offSet);
fsr.setMaxResults(maxResults);

return fsr;
public FieldSearchResult search(Query query, int offSet, int maxResults)
throws LoginException, RepositoryException {

ImmutableList.Builder<ObjectFields> fieldObjects = builder();

//if offSet is 0, we assume it's the first query and set the queryResults
if (offSet == 0) {
queryResults = query.execute();
}

NodeIterator nodeIter = queryResults.getNodes();
int size = (int) nodeIter.getSize();
logger.debug(size + " results found");

//add the next set of results to the fieldObjects starting at offSet for pagination
int i = offSet;
nodeIter.skip((long) offSet);
while (nodeIter.hasNext() && i < (offSet + maxResults)) {
ObjectFields obj = new ObjectFields();
try {
Node node = nodeIter.nextNode();
obj.setPid(node.getName());
obj.setPath(node.getPath());
fieldObjects.add(obj);
} catch (RepositoryException ex) {
logger.error(ex.getMessage());
}
i++;
}

FieldSearchResult fsr =
new FieldSearchResult(fieldObjects.build(), offSet, maxResults,
size);
fsr.setStart(offSet);
fsr.setMaxResults(maxResults);

return fsr;
}
}
Expand Up @@ -30,7 +30,7 @@ public class FedoraIdentifiers extends AbstractResource {

/**
* @param numPids
* @return HTTP 200 with XML-formatted block of PIDs
* @return HTTP 200 with block of PIDs
* @throws RepositoryException
* @throws IOException
* @throws TemplateException
Expand Down
Expand Up @@ -5,7 +5,6 @@
import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_XML;
import static javax.ws.rs.core.Response.created;
import static javax.ws.rs.core.Response.ok;

import java.io.IOException;
import java.net.URI;
Expand Down Expand Up @@ -98,7 +97,7 @@ public Response registerObjectNamespaces(final NamespaceListing nses)
@GET
@Path("/{prefix}")
@Produces(APPLICATION_JSON)
public Response retrieveObjectNamespace(@PathParam("ns")
public Namespace retrieveObjectNamespace(@PathParam("ns")
final String prefix) throws RepositoryException {

final Session session = repo.login();
Expand All @@ -109,7 +108,7 @@ public Response retrieveObjectNamespace(@PathParam("ns")
final Namespace ns =
new Namespace(prefix, URI.create(r.getURI(prefix)));

return ok(ns).build();
return ns;
} finally {
session.logout();
}
Expand Down
2 changes: 1 addition & 1 deletion fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
Expand Up @@ -102,7 +102,7 @@ public Datastream(final Session session, final String dsPath)
this.node.setProperty("jcr:lastModified", Calendar.getInstance());

// TODO: I guess we should also have the PID + DSID..
this.node.setProperty(DC_IDENTIFER,
this.node.setProperty(DC_IDENTIFIER,
new String[] {
this.node.getIdentifier(),
this.node.getParent().getName() + "/" +
Expand Down
2 changes: 1 addition & 1 deletion fcrepo-kernel/src/main/java/org/fcrepo/FedoraObject.java
Expand Up @@ -46,7 +46,7 @@ public FedoraObject(Session session, String path)
node.addMixin(FEDORA_OWNED);
node.setProperty(FEDORA_OWNERID, session.getUserID());
node.setProperty(JCR_LASTMODIFIED, Calendar.getInstance());
node.setProperty(DC_IDENTIFER, new String[] {node.getIdentifier(),
node.setProperty(DC_IDENTIFIER, new String[] {node.getIdentifier(),
node.getName()});
} finally {
context.stop();
Expand Down
Expand Up @@ -21,7 +21,7 @@ public interface FedoraJcrTypes {

public static final String DC_TITLE = "dc:title";

public static final String DC_IDENTIFER = "dc:identifier";
public static final String DC_IDENTIFIER = "dc:identifier";

public static final String JCR_LASTMODIFIED = "jcr:lastModified";

Expand Down
Expand Up @@ -154,7 +154,6 @@ public FixityResult checkFixity(URI checksum, long size,
} catch (CloneNotSupportedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException(e);
}

Expand Down

0 comments on commit 63162f2

Please sign in to comment.