Coverage Summary for Class: LegacyLogicalDocStructHelper (org.kitodo.production.helper.metadata.legacytypeimplementations)

Class Class, % Method, % Line, %
LegacyLogicalDocStructHelper 100% (1/1) 20% (3/15) 19,3% (22/114)


 /*
  * (c) Kitodo. Key to digital objects e. V. <contact@kitodo.org>
  *
  * This file is part of the Kitodo project.
  *
  * It is licensed under GNU General Public License version 3 or later.
  *
  * For the full copyright and license information, please read the
  * GPL3-License.txt file that was distributed with this source code.
  */
 
 package org.kitodo.production.helper.metadata.legacytypeimplementations;
 
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Locale.LanguageRange;
 import java.util.Objects;
 import java.util.Optional;
 
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.kitodo.api.Metadata;
 import org.kitodo.api.MetadataEntry;
 import org.kitodo.api.dataeditor.rulesetmanagement.Domain;
 import org.kitodo.api.dataeditor.rulesetmanagement.MetadataViewInterface;
 import org.kitodo.api.dataeditor.rulesetmanagement.MetadataViewWithValuesInterface;
 import org.kitodo.api.dataeditor.rulesetmanagement.RulesetManagementInterface;
 import org.kitodo.api.dataeditor.rulesetmanagement.StructuralElementViewInterface;
 import org.kitodo.api.dataformat.LogicalDivision;
 import org.kitodo.api.dataformat.PhysicalDivision;
 import org.kitodo.api.dataformat.View;
 import org.kitodo.production.metadata.MetadataEditor;
 
 /**
  * Connects a legacy doc struct from the logical map to an included structural
  * element. This is a soldering class to keep legacy code operational which is
  * about to be removed. Do not use this class.
  */
 public class LegacyLogicalDocStructHelper implements LegacyDocStructHelperInterface, BindingSaveInterface {
     private static final Logger logger = LogManager.getLogger(LegacyLogicalDocStructHelper.class);
 
     /**
      * The logical division accessed via this soldering class.
      */
     private LogicalDivision logicalDivision;
 
     /**
      * The current ruleset.
      */
     private RulesetManagementInterface ruleset;
 
     /**
      * The view on this division.
      */
     private StructuralElementViewInterface divisionView;
 
     /**
      * The user’s metadata language priority list.
      */
     private List<LanguageRange> priorityList;
 
     /**
      * The parent of this class—required by legacy code.
      */
     private LegacyLogicalDocStructHelper parent;
 
     LegacyLogicalDocStructHelper(LogicalDivision logicalDivision, LegacyLogicalDocStructHelper parent,
             RulesetManagementInterface ruleset, List<LanguageRange> priorityList) {
         this.logicalDivision = logicalDivision;
         this.ruleset = ruleset;
         this.priorityList = priorityList;
         this.parent = parent;
         String type = logicalDivision.getType();
         this.divisionView = ruleset.getStructuralElementView(Objects.nonNull(type) ? type : "", "edit", priorityList);
     }
 
     @Override
     @Deprecated
     public void addMetadata(LegacyMetadataHelper metadata) {
         Optional<MetadataViewInterface> optionalKeyView = divisionView
                 .getAddableMetadata(logicalDivision.getMetadata(), Collections.emptyList()).parallelStream()
                 .filter(keyView -> keyView.getId().equals(metadata.getMetadataType().getName())).findFirst();
         Optional<Domain> optionalDomain = optionalKeyView.isPresent() ? optionalKeyView.get().getDomain()
                 : Optional.empty();
         if (!optionalDomain.isPresent() || !optionalDomain.get().equals(Domain.METS_DIV)) {
             MetadataEntry metadataEntry = new MetadataEntry();
             metadata.setBinding(this, metadataEntry, optionalDomain.orElse(Domain.DESCRIPTION));
             metadata.saveToBinding();
             logicalDivision.getMetadata().add(metadataEntry);
         } else {
             metadata.setBinding(this, null, Domain.METS_DIV);
             metadata.saveToBinding();
         }
     }
 
     @Override
     public void saveMetadata(LegacyMetadataHelper metadata) {
         if (Domain.METS_DIV.equals(metadata.getDomain())) {
             try {
                 logicalDivision.getClass().getMethod("set".concat(metadata.getMetadataType().getName()), String.class)
                         .invoke(logicalDivision, metadata.getValue());
             } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                 throw new IllegalArgumentException(e.getMessage(), e);
             }
         } else if (metadata.getBinding() != null) {
             metadata.getBinding().setKey(metadata.getMetadataType().getName());
             metadata.getBinding().setDomain(MetadataEditor.domainToMdSec(metadata.getDomain()));
             metadata.getBinding().setValue(metadata.getValue());
         }
     }
 
     @Override
     @Deprecated
     public LegacyReferenceHelper addReferenceTo(LegacyDocStructHelperInterface docStruct, String type) {
         View view = new View();
         LegacyInnerPhysicalDocStructHelper target = (LegacyInnerPhysicalDocStructHelper) docStruct;
         view.setPhysicalDivision(target.getPhysicalDivision());
         logicalDivision.getViews().add(view);
         view.getPhysicalDivision().getLogicalDivisions().add(logicalDivision);
         return new LegacyReferenceHelper(target);
     }
 
     @Override
     @Deprecated
     public List<LegacyMetadataTypeHelper> getAddableMetadataTypes() {
         Collection<MetadataViewInterface> addableKeys = divisionView.getAddableMetadata(
             logicalDivision.getMetadata(),
             Collections.emptyList());
         ArrayList<LegacyMetadataTypeHelper> addableMetadataTypes = new ArrayList<>(addableKeys.size());
         for (MetadataViewInterface key : addableKeys) {
             addableMetadataTypes.add(new LegacyMetadataTypeHelper(key));
         }
         return addableMetadataTypes;
     }
 
     @Override
     @Deprecated
     public List<LegacyDocStructHelperInterface> getAllChildren() {
         List<LegacyDocStructHelperInterface> wrappedChildren = new ArrayList<>();
         for (LogicalDivision child : logicalDivision.getChildren()) {
             wrappedChildren.add(new LegacyLogicalDocStructHelper(child, this, ruleset, priorityList));
         }
         return wrappedChildren;
     }
 
     @Override
     @Deprecated
     public List<LegacyMetadataHelper> getAllMetadata() {
         List<LegacyMetadataHelper> allMetadata = new LinkedList<>();
         List<MetadataViewWithValuesInterface> entryViews = divisionView
                 .getSortedVisibleMetadata(logicalDivision.getMetadata(), Collections.emptyList());
         for (MetadataViewWithValuesInterface entryView : entryViews) {
             Optional<MetadataViewInterface> optionalMetadata = entryView.getMetadata();
             if (optionalMetadata.isPresent()) {
                 MetadataViewInterface key = optionalMetadata.get();
                 for (Metadata value : entryView.getValues()) {
                     if (value instanceof MetadataEntry) {
                         allMetadata.add(new LegacyMetadataHelper(null, new LegacyMetadataTypeHelper(key),
                                 ((MetadataEntry) value).getValue()));
                     }
                 }
             }
         }
         return allMetadata;
     }
 
     @Override
     @Deprecated
     public List<LegacyMetadataHelper> getAllMetadataByType(LegacyMetadataTypeHelper metadataType) {
         List<LegacyMetadataHelper> allMetadata = new LinkedList<>();
         List<MetadataViewWithValuesInterface> entryViews = divisionView
                 .getSortedVisibleMetadata(logicalDivision.getMetadata(), Collections.emptyList());
         for (MetadataViewWithValuesInterface entryView : entryViews) {
             if (entryView.getMetadata().isPresent()) {
                 Optional<MetadataViewInterface> optionalMetadata = entryView.getMetadata();
                 MetadataViewInterface key = optionalMetadata.get();
                 if (key.getId().equals(metadataType.getName())) {
                     for (Metadata value : entryView.getValues()) {
                         if (value instanceof MetadataEntry) {
                             allMetadata.add(new LegacyMetadataHelper(null, new LegacyMetadataTypeHelper(key),
                                     ((MetadataEntry) value).getValue()));
                         }
                     }
                 }
             }
         }
         return allMetadata;
     }
 
     @Override
     @Deprecated
     public Collection<LegacyReferenceHelper> getAllToReferences(String type) {
         switch (type) {
             case "logical_physical":
                 Collection<View> views = logicalDivision.getViews();
                 ArrayList<LegacyReferenceHelper> allReferences = new ArrayList<>(views.size());
                 for (View view : views) {
                     PhysicalDivision physicalDivision = view.getPhysicalDivision();
                     allReferences.add(new LegacyReferenceHelper(new LegacyInnerPhysicalDocStructHelper(physicalDivision)));
                 }
                 return allReferences;
             default:
                 throw new IllegalArgumentException("Unknown reference type: " + type);
         }
     }
 
     /**
      * Metadata eines Docstructs ermitteln.
      *
      * @param inStruct
      *            DocStruct object
      * @param inMetadataType
      *            MetadataType object
      * @return Metadata
      */
     @Deprecated
     public static LegacyMetadataHelper getMetadata(LegacyDocStructHelperInterface inStruct, LegacyMetadataTypeHelper inMetadataType) {
         if (inStruct != null && inMetadataType != null) {
             List<? extends LegacyMetadataHelper> all = inStruct.getAllMetadataByType(inMetadataType);
             if (all.isEmpty()) {
                 LegacyMetadataHelper md = new LegacyMetadataHelper(inMetadataType);
                 md.setDocStruct(inStruct);
                 inStruct.addMetadata(md);
                 return md;
             } else {
                 return all.get(0);
             }
         }
         return null;
     }
 
     @Override
     @Deprecated
     public LegacyLogicalDocStructTypeHelper getDocStructType() {
         return new LegacyLogicalDocStructTypeHelper(divisionView);
     }
 
     /**
      * This method is not part of the interface, but the JSF code digs in the
      * depths of the UGH and uses it on the guts.
      *
      * @return Method delegated to {@link #getDocStructType()}
      */
     @Deprecated
     public LegacyLogicalDocStructTypeHelper getType() {
         if (!logger.isTraceEnabled()) {
             StackTraceElement[] stackTrace = new RuntimeException().getStackTrace();
             logger.log(Level.WARN, "Method {}.{}() invokes {}.{}(), bypassing the interface!",
                 stackTrace[1].getClassName(), stackTrace[1].getMethodName(), stackTrace[0].getClassName(),
                 stackTrace[0].getMethodName());
         }
         return getDocStructType();
     }
 
     @Deprecated
     public boolean isDocStructTypeAllowedAsChild(LegacyLogicalDocStructTypeHelper type) {
         return divisionView.getAllowedSubstructuralElements().containsKey(type.getName());
     }
 
     @Override
     @Deprecated
     public void removeMetadata(LegacyMetadataHelper metaDatum) {
         Iterator<Metadata> entries = logicalDivision.getMetadata().iterator();
         String metadataTypeName = metaDatum.getMetadataType().getName();
         while (entries.hasNext()) {
             Metadata entry = entries.next();
             if (entry.getKey().equals(metadataTypeName)
                     && ((MetadataEntry) entry).getValue().equals(metaDatum.getValue())) {
                 entries.remove();
                 break;
             }
         }
     }
 }