Skip to content

Commit

Permalink
Remove nested try-catch blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
osmandin authored and Andrew Woods committed Feb 19, 2015
1 parent 561036d commit 3fa9652
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 58 deletions.
Expand Up @@ -345,23 +345,20 @@ public Response updateSparql(@ContentLocation final InputStream requestBodyStrea
LOGGER.info("PATCH for '{}'", externalPath);
patchResourcewithSparql(resource(), requestBody, resourceTriples);

try {
session.save();
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
session.save();

addCacheControlHeaders(servletResponse, resource(), session);

return noContent().build();

} catch ( final RuntimeException ex ) {
final Throwable cause = ex.getCause();
if ( cause != null && cause instanceof PathNotFoundException) {
// the sparql update referred to a repository resource that doesn't exist
throw new BadRequestException(cause.getMessage());
}
throw ex;
} catch (final RepositoryException e) {
throw new RepositoryRuntimeException(e);
}
}

Expand Down
Expand Up @@ -264,33 +264,17 @@ private Node getNode(final String path) throws RepositoryException {
*/
private Node getFrozenNodeByLabel(final String baseResourcePath, final String label) {
try {
try {
final Node frozenNode = session.getNodeByIdentifier(label);
final Node n = getNode(baseResourcePath, label);

/*
* We found a node whose identifier is the "label" for the version. Now
* we must do due dilligence to make sure it's a frozen node representing
* a version of the subject node.
*/
final Property p = frozenNode.getProperty("jcr:frozenUuid");
if (p != null) {
final Node subjectNode = session.getNode(baseResourcePath);
if (p.getString().equals(subjectNode.getIdentifier())) {
return frozenNode;
}
}
/*
if (n != null) {
return n;
}

/*
* Though a node with an id of the label was found, it wasn't the
* node we were looking for, so fall through and look for a labeled
* node.
*/
} catch (final ItemNotFoundException ex) {
/*
* the label wasn't a uuid of a frozen node but
* instead possibly a version label.
*/
}

final VersionHistory hist =
session.getWorkspace().getVersionManager().getVersionHistory(baseResourcePath);
if (hist.hasVersionLabel(label)) {
Expand All @@ -305,6 +289,32 @@ private Node getFrozenNodeByLabel(final String baseResourcePath, final String la
}
}

private Node getNode(final String baseResourcePath, final String label) throws RepositoryException {
try {
final Node frozenNode = session.getNodeByIdentifier(label);

/*
* We found a node whose identifier is the "label" for the version. Now
* we must do due diligence to make sure it's a frozen node representing
* a version of the subject node.
*/
final Property p = frozenNode.getProperty("jcr:frozenUuid");
if (p != null) {
final Node subjectNode = session.getNode(baseResourcePath);
if (p.getString().equals(subjectNode.getIdentifier())) {
return frozenNode;
}
}

} catch (final ItemNotFoundException ex) {
/*
* the label wasn't a uuid of a frozen node but
* instead possibly a version label.
*/
}
return null;
}

private static String getPath(final FedoraResource resource) {
if (isFrozenNode.apply(resource)) {
try {
Expand Down
Expand Up @@ -567,33 +567,11 @@ public FedoraResource getUnfrozenResource() {
public Node getNodeVersion(final String label) {
try {
final Session session = getSession();
try {

final Node frozenNode = session.getNodeByIdentifier(label);

final String baseUUID = getNode().getIdentifier();
final Node n = getFrozenNode(label);

/*
* We found a node whose identifier is the "label" for the version. Now
* we must do due dilligence to make sure it's a frozen node representing
* a version of the subject node.
*/
final Property p = frozenNode.getProperty("jcr:frozenUuid");
if (p != null) {
if (p.getString().equals(baseUUID)) {
return frozenNode;
}
}
/*
* Though a node with an id of the label was found, it wasn't the
* node we were looking for, so fall through and look for a labeled
* node.
*/
} catch (final ItemNotFoundException ex) {
/*
* the label wasn't a uuid of a frozen node but
* instead possibly a version label.
*/
if (n != null) {
return n;
}

if (isVersioned()) {
Expand All @@ -614,6 +592,39 @@ public Node getNodeVersion(final String label) {

}

private Node getFrozenNode(final String label) throws RepositoryException {
try {
final Session session = getSession();

final Node frozenNode = session.getNodeByIdentifier(label);

final String baseUUID = getNode().getIdentifier();

/*
* We found a node whose identifier is the "label" for the version. Now
* we must do due dilligence to make sure it's a frozen node representing
* a version of the subject node.
*/
final Property p = frozenNode.getProperty("jcr:frozenUuid");
if (p != null) {
if (p.getString().equals(baseUUID)) {
return frozenNode;
}
}
/*
* Though a node with an id of the label was found, it wasn't the
* node we were looking for, so fall through and look for a labeled
* node.
*/
} catch (final ItemNotFoundException ex) {
/*
* the label wasn't a uuid of a frozen node but
* instead possibly a version label.
*/
}
return null;
}

@Override
public boolean equals(final Object object) {
if (object instanceof FedoraResourceImpl) {
Expand Down
Expand Up @@ -144,11 +144,7 @@ public void removedStatement(final Statement s) {

if (property.equals(RDF.type) && objectNode.isResource()) {
final Resource mixinResource = objectNode.asResource();
try {
jcrRdfTools.removeMixin(resource, mixinResource, s.getModel().getNsPrefixMap());
} catch (final RepositoryException e) {
// TODO
}
jcrRdfTools.removeMixin(resource, mixinResource, s.getModel().getNsPrefixMap());
return;
}

Expand Down

0 comments on commit 3fa9652

Please sign in to comment.