Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Genesys PGR
Genesys Backend
Commits
c7f90072
Commit
c7f90072
authored
Dec 22, 2013
by
Matija Obreza
Browse files
REST /api/v0/acn/{instCode}/update accepts array of accessions for
MLSSTAT update
parent
0b04bf65
Changes
1
Show whitespace changes
Inline
Side-by-side
src/main/java/org/genesys2/server/servlet/controller/rest/AccessionController.java
View file @
c7f90072
...
...
@@ -17,12 +17,15 @@
package
org.genesys2.server.servlet.controller.rest
;
import
java.io.IOException
;
import
java.util.Iterator
;
import
java.util.Map.Entry
;
import
java.text.MessageFormat
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.genesys2.server.model.genesys.Accession
;
import
org.genesys2.server.model.impl.AccessionIdentifier3
;
import
org.genesys2.server.service.GenesysService
;
import
org.genesys2.spring.ResourceNotFoundException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.MediaType
;
import
org.springframework.security.access.prepost.PreAuthorize
;
...
...
@@ -37,6 +40,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
@Controller
(
"restAccessionController"
)
@PreAuthorize
(
"isAuthenticated()"
)
...
...
@@ -80,58 +84,99 @@ public class AccessionController extends RestController {
boolean
updateInstituteAccession
(
@PathVariable
(
"instCode"
)
String
instCode
,
@RequestBody
String
content
)
throws
JsonProcessingException
,
IOException
{
// TODO Check user's permissions to update this WIEWS institute.
JsonNode
json
=
mapper
.
readTree
(
content
);
Map
<
DataJson
,
ObjectNode
>
batch
=
new
HashMap
<
DataJson
,
ObjectNode
>();
String
genus
=
null
;
String
acceNumb
=
null
;
for
(
Iterator
<
Entry
<
String
,
JsonNode
>>
fields
=
json
.
fields
();
fields
.
hasNext
();)
{
Entry
<
String
,
JsonNode
>
field
=
fields
.
next
();
String
key
=
field
.
getKey
();
JsonNode
value
=
field
.
getValue
();
if
(
"genus"
.
equals
(
key
))
{
genus
=
value
.
textValue
();
}
else
if
(
"acceNumb"
.
equals
(
key
))
{
acceNumb
=
value
.
textValue
();
if
(
json
.
isArray
())
{
for
(
JsonNode
j
:
json
)
{
DataJson
dataJson
=
readAid3
(
j
);
if
(!
instCode
.
equals
(
dataJson
.
instCode
))
{
throw
new
RuntimeException
(
"Accession does not belong to instCode="
+
instCode
+
" acn="
+
dataJson
);
}
batch
.
put
(
dataJson
,
(
ObjectNode
)
j
);
}
}
else
{
DataJson
dataJson
=
readAid3
(
json
);
if
(!
instCode
.
equals
(
dataJson
.
instCode
))
{
throw
new
RuntimeException
(
"Accession does not belong to instCode="
+
instCode
+
" acn="
+
dataJson
);
}
batch
.
put
(
dataJson
,
(
ObjectNode
)
json
);
}
if
(
LOG
.
i
sDebugEnabled
())
LOG
.
debug
(
"Loading accession "
+
instCode
+
"."
+
acceNumb
+
" genus="
+
genus
);
LOG
.
i
nfo
(
"Batch processing "
+
batch
.
size
()
+
" entries"
);
List
<
Accession
>
toSave
=
new
ArrayList
<
Accession
>(
);
for
(
DataJson
dataJson
:
batch
.
keySet
())
{
if
(
LOG
.
isDebugEnabled
())
LOG
.
debug
(
"Loading accession "
+
dataJson
);
Accession
accession
=
genesysService
.
getAccession
(
instCode
,
genus
,
acceNumb
);
Accession
accession
=
genesysService
.
getAccession
(
dataJson
.
instCode
,
dataJson
.
genus
,
dataJson
.
acceNumb
);
if
(
accession
==
null
)
{
LOG
.
warn
(
"No accession "
+
instCode
+
"."
+
acceNumb
+
" genus="
+
genus
);
throw
new
ResourceNotFoundException
()
;
//
LOG.warn("No accession " +
dataJson
);
continue
;
}
ObjectNode
accnJson
=
batch
.
get
(
dataJson
);
boolean
updated
=
false
;
for
(
Iterator
<
Entry
<
String
,
JsonNode
>>
fields
=
json
.
fields
();
fields
.
hasNext
();)
{
Entry
<
String
,
JsonNode
>
field
=
fields
.
next
();
String
key
=
field
.
getKey
();
JsonNode
value
=
field
.
getValue
();
if
(
"mlsStat"
.
equals
(
key
))
{
JsonNode
value
=
accnJson
.
get
(
"mlsStat"
);
if
(
value
!=
null
)
{
Boolean
inMls
=
value
.
isNull
()
?
null
:
value
.
asBoolean
();
if
(
inMls
!=
accession
.
getMlsStatus
())
{
if
(
LOG
.
is
Info
Enabled
())
LOG
.
info
(
"Setting MLSSTAT to "
+
inMls
);
if
(
LOG
.
is
Debug
Enabled
())
LOG
.
debug
(
"Setting MLSSTAT to "
+
inMls
);
accession
.
setMlsStatus
(
inMls
);
updated
=
true
;
}
}
}
if
(
updated
)
{
genesysService
.
saveAccession
(
accession
);
toSave
.
add
(
accession
);
}
}
if
(
toSave
.
size
()
>
0
)
{
LOG
.
info
(
"Storing "
+
toSave
.
size
()
+
" accessions."
);
genesysService
.
saveAccessions
(
toSave
);
}
return
toSave
.
size
()
>
0
;
}
private
DataJson
readAid3
(
JsonNode
json
)
{
DataJson
dataJson
=
new
DataJson
();
dataJson
.
instCode
=
json
.
has
(
"instCode"
)
?
json
.
get
(
"instCode"
).
textValue
()
:
null
;
dataJson
.
acceNumb
=
json
.
has
(
"acceNumb"
)
?
json
.
get
(
"acceNumb"
).
textValue
()
:
null
;
dataJson
.
genus
=
json
.
has
(
"genus"
)
?
json
.
get
(
"genus"
).
textValue
()
:
null
;
return
dataJson
;
}
return
updated
;
public
static
class
DataJson
implements
AccessionIdentifier3
{
public
String
instCode
;
public
String
acceNumb
;
public
String
genus
;
@Override
public
String
getHoldingInstitute
()
{
return
instCode
;
}
@Override
public
String
getAccessionName
()
{
return
acceNumb
;
}
@Override
public
String
getGenus
()
{
return
genus
;
}
@Override
public
String
toString
()
{
return
MessageFormat
.
format
(
"AID3 instCode={0} acceNumb={1} genus={2}"
,
instCode
,
acceNumb
,
genus
);
}
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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