@@ -800,7 +800,7 @@ public Response autopopulate(@QueryParam("repo") String url,
800
800
* owned by User
801
801
* @throws InvocationTargetException on reflection errors
802
802
*/
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 ,
804
804
IllegalAccessException , InvocationTargetException {
805
805
// fix the open source value before storing
806
806
md .setOpenSource (
@@ -815,6 +815,9 @@ private void store(EntityManager em, DOECodeMetadata md, User user) throws NotFo
815
815
if (projectKeywords != null && !projectKeywords .isEmpty () && !user .hasRole ("RecordAdmin" ) && !user .hasRole ("ApprovalAdmin" ))
816
816
throw new ValidationException ("Project Keywords can only be set by authorized users." );
817
817
818
+ // has FileName?
819
+ boolean hasFilename = !StringUtils .isBlank (md .getFileName ());
820
+
818
821
// if there's a CODE ID, attempt to look up the record first and
819
822
// copy attributes into it
820
823
if ( null ==md .getCodeId () || 0 ==md .getCodeId ()) {
@@ -829,6 +832,11 @@ private void store(EntityManager em, DOECodeMetadata md, User user) throws NotFo
829
832
throw new BadRequestException (ErrorResponse .badRequest (reasons ).build ());
830
833
}
831
834
835
+ if (hasFilename && !hasFile ) {
836
+ // trying to set a filename without a file, ignore the filename
837
+ md .setFileName (null );
838
+ }
839
+
832
840
// log changes
833
841
ChangeLog cl = new ChangeLog ();
834
842
cl .setChangedBy (user .getEmail ());
@@ -839,6 +847,11 @@ private void store(EntityManager em, DOECodeMetadata md, User user) throws NotFo
839
847
} else {
840
848
DOECodeMetadata emd = em .find (DOECodeMetadata .class , md .getCodeId ());
841
849
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
+
842
855
if ( null !=emd ) {
843
856
// to Approve, user must be an ApprovalAdmin and record must be previously Submitted/Announced
844
857
if (DOECodeMetadata .Status .Approved .equals (md .getWorkflowStatus ())) {
@@ -1682,13 +1695,16 @@ private Response doSave(String json, InputStream file, FormDataContentDispositio
1682
1695
1683
1696
md .setLastEditor (md .getOwner ());
1684
1697
1685
- store (em , md , user );
1698
+ // was file uploaded?
1699
+ boolean hasFile = null !=file && null !=fileInfo ;
1700
+
1701
+ store (em , md , user , hasFile );
1686
1702
1687
1703
// re-attach metadata to transaction in order to store any changes beyond this point
1688
1704
md = em .find (DOECodeMetadata .class , md .getCodeId ());
1689
1705
1690
1706
// if there's a FILE associated here, store it
1691
- if ( null != file && null != fileInfo ) {
1707
+ if ( hasFile ) {
1692
1708
try {
1693
1709
String fileName = writeFile (file , md .getCodeId (), fileInfo .getFileName (), FILE_UPLOADS );
1694
1710
md .setFileName (fileName );
@@ -1821,15 +1837,18 @@ private Response doSubmit(String json, InputStream file, FormDataContentDisposit
1821
1837
1822
1838
md .setLastEditor (md .getOwner ());
1823
1839
1840
+ // was file uploaded?
1841
+ boolean hasFile = null !=file && null !=fileInfo ;
1842
+
1824
1843
// store it
1825
- store (em , md , user );
1844
+ store (em , md , user , hasFile );
1826
1845
1827
1846
// re-attach metadata to transaction in order to store any changes beyond this point
1828
1847
md = em .find (DOECodeMetadata .class , md .getCodeId ());
1829
1848
1830
1849
// if there's a FILE associated here, store it
1831
1850
String fullFileName = "" ;
1832
- if ( null != file && null != fileInfo ) {
1851
+ if ( hasFile ) {
1833
1852
try {
1834
1853
fullFileName = writeFile (file , md .getCodeId (), fileInfo .getFileName (), FILE_UPLOADS );
1835
1854
md .setFileName (fullFileName );
@@ -2015,15 +2034,18 @@ private Response doAnnounce(String json, InputStream file, FormDataContentDispos
2015
2034
md .setDoi (reservation .getReservedDoi ());
2016
2035
}
2017
2036
2037
+ // was file uploaded?
2038
+ boolean hasFile = null !=file && null !=fileInfo ;
2039
+
2018
2040
// persist this to the database
2019
- store (em , md , user );
2041
+ store (em , md , user , hasFile );
2020
2042
2021
2043
// re-attach metadata to transaction in order to store any changes beyond this point
2022
2044
md = em .find (DOECodeMetadata .class , md .getCodeId ());
2023
2045
2024
2046
// if there's a FILE associated here, store it
2025
2047
String fullFileName = "" ;
2026
- if ( null != file && null != fileInfo ) {
2048
+ if ( hasFile ) {
2027
2049
try {
2028
2050
fullFileName = writeFile (file , md .getCodeId (), fileInfo .getFileName (), FILE_UPLOADS );
2029
2051
md .setFileName (fullFileName );
@@ -2392,7 +2414,7 @@ public Response approve(@PathParam("codeId") Long codeId) {
2392
2414
md .setWorkflowStatus (Status .Approved );
2393
2415
2394
2416
// 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 );
2396
2418
2397
2419
// send any updates to DataCite as well (if RELEASE DATE is set)
2398
2420
if (StringUtils .isNotEmpty (md .getDoi ()) && null !=md .getReleaseDate ()) {
0 commit comments