Skip to content

Commit

Permalink
Added a new tag into submission-forms.xml called default-value. (#699)
Browse files Browse the repository at this point in the history
* Added a new tag into submission-forms.xml called `default-value`. This default value is assigned into Item during sending a sectionData to the FE.

* Cherry picked fixed randomly failing IT: ManageGroupsFeatureIT, LinksetRestControllerIT (#701)

Co-authored-by: Tim Donohue <tim.donohue@lyrasis.org>

---------

Co-authored-by: Tim Donohue <tim.donohue@lyrasis.org>
  • Loading branch information
milanmajchrak and tdonohue committed Jul 18, 2024
1 parent 108d254 commit 9c1b2d2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
24 changes: 24 additions & 0 deletions dspace-api/src/main/java/org/dspace/app/util/DCInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ public class DCInput {
*/
private ComplexDefinition complexDefinition = null;

/**
* the dropdown input type could have defined a default value
*/
private String defaultValue = "";

private boolean isRelationshipField = false;
private boolean isMetadataField = false;
private String relationshipType = null;
Expand Down Expand Up @@ -264,6 +269,7 @@ public DCInput(Map<String, String> fieldMap, Map<String, List<String>> listMap,
externalSources.add(StringUtils.trim(source));
}
}
defaultValue = fieldMap.get("default-value");

}

Expand Down Expand Up @@ -607,6 +613,24 @@ public ComplexDefinition getComplexDefinition() {
return this.complexDefinition;
}

public String getDefaultValue() {
return defaultValue;
}

public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}

public boolean hasDefaultValue() {
return StringUtils.isNotEmpty(this.getDefaultValue());
}

public boolean isDropdownValue() {
return "dropdown".equals(getInputType());
}



/**
* Convert complex definition HashMap to the ordered JSON string
* @return complex definition in the JSON string which will be parsed in the FE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package org.dspace.app.rest.submit.step;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
Expand Down Expand Up @@ -35,19 +36,22 @@
import org.dspace.app.rest.submit.SubmissionService;
import org.dspace.app.rest.submit.factory.PatchOperationFactory;
import org.dspace.app.rest.submit.factory.impl.PatchOperation;
import org.dspace.app.rest.utils.ContextUtil;
import org.dspace.app.util.DCInput;
import org.dspace.app.util.DCInputSet;
import org.dspace.app.util.DCInputsReader;
import org.dspace.app.util.DCInputsReaderException;
import org.dspace.app.util.SubmissionStepConfig;
import org.dspace.content.InProgressSubmission;
import org.dspace.content.Item;
import org.dspace.content.MetadataValue;
import org.dspace.content.RelationshipMetadataService;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.core.Context;
import org.dspace.core.Utils;
import org.dspace.services.ConfigurationService;
import org.dspace.services.factory.DSpaceServicesFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

/**
Expand Down Expand Up @@ -118,6 +122,19 @@ private void readField(InProgressSubmission obj, SubmissionStepConfig config, Da
for (String fieldName : fieldsName) {
List<MetadataValue> mdv = itemService.getMetadataByMetadataString(obj.getItem(),
fieldName);
// Use default value if the input type is dropdown and has defined a default value
if (CollectionUtils.isEmpty(mdv) && input.isDropdownValue() && input.hasDefaultValue()) {
Context context = ContextUtil.obtainCurrentRequestContext();
try {
MetadataValue mv = itemService.addMetadata(context, obj.getItem(), input.getSchema(),
input.getElement(), input.getQualifier(), Item.ANY, input.getDefaultValue());
mdv.add(mv);
} catch (SQLException e) {
log.error("Cannot create a metadata value object with the default value, because: " +
"{}", e.getMessage());
throw new RuntimeException(e);
}
}
for (MetadataValue md : mdv) {
MetadataValueRest dto = new MetadataValueRest();
dto.setAuthority(md.getAuthority());
Expand Down
4 changes: 3 additions & 1 deletion dspace/config/submission-forms.dtd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<!ELEMENT row (field|relation-field)+ >
<!ATTLIST form name NMTOKEN #REQUIRED>

<!ELEMENT field (dc-schema, dc-element, dc-qualifier?, language?, repeatable?, label, style?, type-bind?, input-type, hint, required?, regex?, vocabulary?, visibility?, readonly?, acl?) >
<!ELEMENT field (dc-schema, dc-element, dc-qualifier?, language?, repeatable?, label, style?, type-bind?, input-type, default-value?, hint, required?, regex?, vocabulary?, visibility?, readonly?, acl?) >
<!ELEMENT definition (input*) >
<!ATTLIST definition
name CDATA #REQUIRED
Expand All @@ -25,6 +25,7 @@
<!ELEMENT label (#PCDATA) >
<!ELEMENT style (#PCDATA) >
<!ELEMENT input-type (#PCDATA)>
<!ELEMENT default-value (#PCDATA)>

<!ELEMENT input (#PCDATA) >
<!ATTLIST input input-type CDATA #REQUIRED >
Expand All @@ -41,6 +42,7 @@
<!ATTLIST input mapped-to-if-not-default CDATA #IMPLIED >
<!ATTLIST input value CDATA #IMPLIED >
<!ATTLIST input required CDATA #IMPLIED >
<!ATTLIST input default-value CDATA #IMPLIED >

<!ELEMENT hint (#PCDATA) >
<!ELEMENT required (#PCDATA)>
Expand Down
2 changes: 2 additions & 0 deletions dspace/config/submission-forms.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2023,7 +2023,9 @@
<dc-qualifier/>
<repeatable>false</repeatable>
<label>The type of the resource</label>
<style>d-none</style>
<input-type value-pairs-name="teaching_materials">dropdown</input-type>
<default-value>teachingMaterials</default-value>
<hint>This is here to autofill a value. The value should not be changed.</hint>
<required>Please select a resource type for your submission.</required>
</field>
Expand Down

0 comments on commit 9c1b2d2

Please sign in to comment.