1111
1212package org .kitodo .production .forms .dataeditor ;
1313
14+ import java .io .IOException ;
15+ import java .io .Serializable ;
1416import java .util .HashSet ;
1517import java .util .LinkedList ;
1618import java .util .List ;
19+ import java .util .Objects ;
1720
21+ import javax .faces .view .ViewScoped ;
22+ import javax .inject .Inject ;
23+ import javax .inject .Named ;
24+
25+ import org .apache .logging .log4j .LogManager ;
26+ import org .apache .logging .log4j .Logger ;
1827import org .kitodo .api .Metadata ;
28+ import org .kitodo .api .dataformat .Division ;
29+ import org .kitodo .api .dataformat .LogicalDivision ;
1930import org .kitodo .exceptions .InvalidMetadataValueException ;
2031import org .kitodo .production .forms .createprocess .ProcessDetail ;
2132import org .kitodo .production .helper .Helper ;
2435import org .primefaces .PrimeFaces ;
2536import org .primefaces .model .TreeNode ;
2637
27- public class UpdateMetadataDialog {
38+ /**
39+ * Manages the dialog when a user clicks on the update metadata button.
40+ *
41+ * <p>Re-imports the catalog record for a known record identifier and import configuration
42+ * and visualizes the differences between the current metadata and re-imported metadata of the
43+ * catalog.</p>
44+ */
45+ @ Named ("UpdateMetadataDialog" )
46+ @ ViewScoped
47+ public class UpdateMetadataDialog implements Serializable {
48+
49+ private static final Logger logger = LogManager .getLogger (UpdateMetadataDialog .class );
2850
29- private final DataEditorForm dataEditor ;
51+ @ Inject
52+ private DataEditorForm dataEditor ;
3053
3154 private List <MetadataComparison > metadataComparisons = new LinkedList <>();
3255
33- UpdateMetadataDialog (DataEditorForm dataEditor ) {
34- this .dataEditor = dataEditor ;
35- }
56+ private String recordIdentifier = "" ;
3657
3758 /**
3859 * Get list of metadata comparisons displayed in 'UpdateMetadataDialog'.
@@ -43,11 +64,73 @@ public List<MetadataComparison> getMetadataComparisons() {
4364 return metadataComparisons ;
4465 }
4566
67+ /**
68+ * Return catalog identifier of record whose metadata is imported.
69+ *
70+ * <p>The record identifier is shown to the user in the metadata comparison dialog.</p>
71+ *
72+ * @return the record identifier
73+ */
74+ public String getRecordIdentifier () {
75+ return this .recordIdentifier ;
76+ }
77+
78+ /**
79+ * Set record identifier of record whose metadata is imported.
80+ *
81+ * <p>The record identifier is shown to the user in the metadata comparison dialog.</p>
82+ *
83+ * @param recordIdentifier the record identifier
84+ */
85+ public void setRecordIdentifier (String recordIdentifier ) {
86+ this .recordIdentifier = recordIdentifier ;
87+ }
88+
89+ /**
90+ * Perform metadata update for current process.
91+ */
92+ public void applyMetadataUpdate () {
93+ Division <?> division = dataEditor .getMetadataPanel ().getLogicalMetadataTable ().getDivision ();
94+ if (Objects .nonNull (division ) && division instanceof LogicalDivision ) {
95+ DataEditorService .updateMetadataWithNewValues ((LogicalDivision ) division , getMetadataComparisons ());
96+ dataEditor .getMetadataPanel ().update ();
97+ } else {
98+ Helper .setErrorMessage ("cannot update metadata of non-logical division" );
99+ }
100+ }
101+
102+ /**
103+ * Check and return whether conditions for metadata update are met or not.
104+ *
105+ * @return whether metadata of process can be updated
106+ */
107+ public boolean canUpdateMetadata () {
108+ try {
109+ return DataEditorService .canUpdateCatalogMetadata (
110+ dataEditor .getProcess (), dataEditor .getWorkpiece (), dataEditor .getStructurePanel ().getSelectedLogicalNodeIfSingle ()
111+ );
112+ } catch (IOException e ) {
113+ Helper .setErrorMessage (e .getLocalizedMessage (), logger , e );
114+ return false ;
115+ }
116+ }
117+
118+ /**
119+ * Is called when a user clicks on the update metadata button of the logical metadata panel.
120+ */
121+ public void onUpdateCatalogMetadataClick () {
122+ if (canUpdateMetadata ()) {
123+ // update metadata from catalog using existing record identifier and import configuration
124+ updateCatalogMetadata ();
125+ }
126+ }
127+
46128 /**
47129 * Trigger re-import of metadata of current process.
48130 */
49131 public void updateCatalogMetadata () {
50132 if (dataEditor .getSelectedStructure ().isPresent ()) {
133+ setRecordIdentifier (dataEditor .getProcessRecordIdentifier ());
51134 try {
52135 HashSet <Metadata > existingMetadata = getMetadata (dataEditor .getMetadataPanel ().getLogicalMetadataRows ());
53136 metadataComparisons = DataEditorService .reimportCatalogMetadata (dataEditor .getProcess (),
0 commit comments