Skip to content
This repository was archived by the owner on Oct 6, 2023. It is now read-only.

Commit c038fca

Browse files
author
Tim Sowers
committed
Keep file & filename synced; (DOECODE-1204)
1 parent dfb1231 commit c038fca

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/main/java/gov/osti/services/Metadata.java

+30-8
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ public Response autopopulate(@QueryParam("repo") String url,
800800
* owned by User
801801
* @throws InvocationTargetException on reflection errors
802802
*/
803-
private void store(EntityManager em, DOECodeMetadata md, User user) throws NotFoundException,
803+
private void store(EntityManager em, DOECodeMetadata md, User user, boolean hasFile) throws NotFoundException,
804804
IllegalAccessException, InvocationTargetException {
805805
// fix the open source value before storing
806806
md.setOpenSource(
@@ -815,6 +815,9 @@ private void store(EntityManager em, DOECodeMetadata md, User user) throws NotFo
815815
if (projectKeywords != null && !projectKeywords.isEmpty() && !user.hasRole("RecordAdmin") && !user.hasRole("ApprovalAdmin"))
816816
throw new ValidationException("Project Keywords can only be set by authorized users.");
817817

818+
// has FileName?
819+
boolean hasFilename = !StringUtils.isBlank(md.getFileName());
820+
818821
// if there's a CODE ID, attempt to look up the record first and
819822
// copy attributes into it
820823
if ( null==md.getCodeId() || 0==md.getCodeId()) {
@@ -829,6 +832,11 @@ private void store(EntityManager em, DOECodeMetadata md, User user) throws NotFo
829832
throw new BadRequestException (ErrorResponse.badRequest(reasons).build());
830833
}
831834

835+
if (hasFilename && !hasFile) {
836+
// trying to set a filename without a file, ignore the filename
837+
md.setFileName(null);
838+
}
839+
832840
// log changes
833841
ChangeLog cl = new ChangeLog();
834842
cl.setChangedBy(user.getEmail());
@@ -839,6 +847,11 @@ private void store(EntityManager em, DOECodeMetadata md, User user) throws NotFo
839847
} else {
840848
DOECodeMetadata emd = em.find(DOECodeMetadata.class, md.getCodeId());
841849

850+
if (hasFilename && !hasFile) {
851+
// trying to set a filename without a file, keep whatever is in database already
852+
md.setFileName(emd.getFileName());
853+
}
854+
842855
if ( null!=emd ) {
843856
// to Approve, user must be an ApprovalAdmin and record must be previously Submitted/Announced
844857
if (DOECodeMetadata.Status.Approved.equals(md.getWorkflowStatus())) {
@@ -1682,13 +1695,16 @@ private Response doSave(String json, InputStream file, FormDataContentDispositio
16821695

16831696
md.setLastEditor(md.getOwner());
16841697

1685-
store(em, md, user);
1698+
// was file uploaded?
1699+
boolean hasFile = null!=file && null!=fileInfo;
1700+
1701+
store(em, md, user, hasFile);
16861702

16871703
// re-attach metadata to transaction in order to store any changes beyond this point
16881704
md = em.find(DOECodeMetadata.class, md.getCodeId());
16891705

16901706
// if there's a FILE associated here, store it
1691-
if ( null!=file && null!=fileInfo ) {
1707+
if ( hasFile ) {
16921708
try {
16931709
String fileName = writeFile(file, md.getCodeId(), fileInfo.getFileName(), FILE_UPLOADS);
16941710
md.setFileName(fileName);
@@ -1821,15 +1837,18 @@ private Response doSubmit(String json, InputStream file, FormDataContentDisposit
18211837

18221838
md.setLastEditor(md.getOwner());
18231839

1840+
// was file uploaded?
1841+
boolean hasFile = null!=file && null!=fileInfo;
1842+
18241843
// store it
1825-
store(em, md, user);
1844+
store(em, md, user, hasFile);
18261845

18271846
// re-attach metadata to transaction in order to store any changes beyond this point
18281847
md = em.find(DOECodeMetadata.class, md.getCodeId());
18291848

18301849
// if there's a FILE associated here, store it
18311850
String fullFileName = "";
1832-
if ( null!=file && null!=fileInfo ) {
1851+
if ( hasFile ) {
18331852
try {
18341853
fullFileName = writeFile(file, md.getCodeId(), fileInfo.getFileName(), FILE_UPLOADS);
18351854
md.setFileName(fullFileName);
@@ -2015,15 +2034,18 @@ private Response doAnnounce(String json, InputStream file, FormDataContentDispos
20152034
md.setDoi(reservation.getReservedDoi());
20162035
}
20172036

2037+
// was file uploaded?
2038+
boolean hasFile = null!=file && null!=fileInfo;
2039+
20182040
// persist this to the database
2019-
store(em, md, user);
2041+
store(em, md, user, hasFile);
20202042

20212043
// re-attach metadata to transaction in order to store any changes beyond this point
20222044
md = em.find(DOECodeMetadata.class, md.getCodeId());
20232045

20242046
// if there's a FILE associated here, store it
20252047
String fullFileName = "";
2026-
if ( null!=file && null!=fileInfo ) {
2048+
if ( hasFile ) {
20272049
try {
20282050
fullFileName = writeFile(file, md.getCodeId(), fileInfo.getFileName(), FILE_UPLOADS);
20292051
md.setFileName(fullFileName);
@@ -2392,7 +2414,7 @@ public Response approve(@PathParam("codeId") Long codeId) {
23922414
md.setWorkflowStatus(Status.Approved);
23932415

23942416
// persist this to the database, as validations should already be complete at this stage.
2395-
store(em, md, user);
2417+
store(em, md, user, false);
23962418

23972419
// send any updates to DataCite as well (if RELEASE DATE is set)
23982420
if (StringUtils.isNotEmpty(md.getDoi()) && null!=md.getReleaseDate()) {

0 commit comments

Comments
 (0)