Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
adapt
ace
Commits
4712098a
Commit
4712098a
authored
Mar 09, 2017
by
Michael Ritter
Browse files
Add initial implementation of a comparison method
parent
71de277c
Changes
1
Hide whitespace changes
Inline
Side-by-side
ace-am/src/main/java/edu/umiacs/ace/rest/CompareController.java
0 → 100644
View file @
4712098a
package
edu.umiacs.ace.rest
;
import
com.google.common.collect.ImmutableList
;
import
edu.umiacs.ace.monitor.core.Collection
;
import
edu.umiacs.ace.monitor.core.MonitoredItem
;
import
edu.umiacs.ace.rest.models.CompareFile
;
import
edu.umiacs.ace.rest.models.CompareRequest
;
import
edu.umiacs.ace.rest.models.CompareResponse
;
import
edu.umiacs.ace.util.PersistUtil
;
import
javax.persistence.EntityManager
;
import
javax.persistence.TypedQuery
;
import
javax.ws.rs.Consumes
;
import
javax.ws.rs.POST
;
import
javax.ws.rs.Path
;
import
javax.ws.rs.PathParam
;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.core.MediaType
;
import
java.util.Objects
;
/**
* Compare files to what ACE knows about a collection
*
* Created by shake on 3/9/17.
*/
@Path
(
"/"
)
public
class
CompareController
{
/**
* API method for comparing files and their digests to a given collection
*
* todo: It would be nice to not have "compare/id" and instead be more along the lines of "collection/id/compare"
* todo: BadRequest if a collection == null?
*
* @param id the id of the collection
* @param request the request containing the comparisons
* @return the result of the comparison
*/
@POST
@Path
(
"compare/{id}"
)
@Consumes
(
MediaType
.
APPLICATION_JSON
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
CompareResponse
compare
(
@PathParam
(
"id"
)
Long
id
,
CompareRequest
request
)
{
if
(
request
==
null
)
{
request
=
new
CompareRequest
();
request
.
setComparisons
(
ImmutableList
.<
CompareFile
>
of
());
}
EntityManager
em
=
PersistUtil
.
getEntityManager
();
CompareResponse
response
=
new
CompareResponse
();
Collection
collection
=
em
.
find
(
Collection
.
class
,
id
);
// can this throw an exception?
for
(
CompareFile
file
:
request
.
getComparisons
())
{
TypedQuery
<
MonitoredItem
>
query
=
em
.
createNamedQuery
(
"MonitoredItem.getItemByPath"
,
MonitoredItem
.
class
);
query
.
setParameter
(
"path"
,
file
.
getPath
());
query
.
setParameter
(
"coll"
,
collection
);
try
{
MonitoredItem
result
=
query
.
getSingleResult
();
if
(
Objects
.
equals
(
result
.
getFileDigest
(),
file
.
getDigest
()))
{
response
.
addMatch
(
file
.
getPath
());
}
else
{
response
.
addDiff
(
file
.
getPath
());
}
}
catch
(
Exception
e
)
{
response
.
addNotFound
(
file
.
getPath
());
}
}
return
response
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment