java/core/src/main/java/se/lth/cs/docria/Document.java
/**
     * Remove text
     * @param text         text to remove, (null is a no-op)
     * @param fieldcascade remove fields in layers referring to this text, cascading removal
     * @return this
     */
Remove text
@param text         text to remove, (null is a no-op)
@param fieldcascade remove fields in layers referring to this text, cascading removal
@return this
Remove text @param text text to remove ( null is a no - op ) @param fieldcascade remove fields in layers referring to this text cascading removal @return this
public Document remove(Text text, boolean fieldcascade) {
        if(text == null)
            return this;
        List<Layer> layersWithReferentFields =
                layers.values()
                      .stream()
                      .filter(l -> l.schema()
                                    .fields()
                                    .values()
                                    .stream()
                                    .anyMatch(dt -> dt.name() == DataTypeName.SPAN && dt.args().get("context").stringValue().equals(text.name()))
                      ).collect(Collectors.toList());
        if(!fieldcascade && !layersWithReferentFields.isEmpty()) {
            String context = layersWithReferentFields.stream()
                                                     .map(l -> l.name()
                                                             + "("
                                                             + l.schema()
                                                                .fields()
                                                                .entrySet()
                                                                .stream()
                                                                .filter(e -> e.getValue().name() == DataTypeName.SPAN
                                                                        && e.getValue().args()
                                                                            .get("context").stringValue()
                                                                            .equals(text.name()))
                                                                .map(Map.Entry::getKey)
                                                                .collect(Collectors.joining(", "))
                                                             + ")")
                                                     .collect(Collectors.joining(", "));
            throw new DataInconsistencyException(String.format("Removing text: %s results in data inconsistency, " +
                                                         "because layers+fields { %s } are referring to this text, " +
                                                         "remove these fields before removing text.", text.name(), context));
        }
        else if(fieldcascade) {
            for (Layer layersWithReferentField : layersWithReferentFields) {
                List<String> fields =
                        layersWithReferentField.schema()
                                               .fields()
                                               .entrySet()
                                               .stream()
                                               .filter(e -> e.getValue().name() == DataTypeName.SPAN && e.getValue().args().get("context").stringValue().equals(text.name()))
                                               .map(Map.Entry::getKey)
                                               .collect(Collectors.toList());
                fields.forEach(layersWithReferentField::removeField);
            }
        }
        return this;
    }
public Document remove ( Text text , boolean fieldcascade ) { if ( text == null ) return this ; List < Layer > layersWithReferentFields = layers . values ( ) . stream ( ) . filter ( l -> l . schema ( ) . fields ( ) . values ( ) . stream ( ) . anyMatch ( dt -> dt . name ( ) == DataTypeName . SPAN && dt . args ( ) . get ( "context" ) . stringValue ( ) . equals ( text . name ( ) ) ) ) . collect ( Collectors . toList ( ) ) ; if ( ! fieldcascade && ! layersWithReferentFields . isEmpty ( ) ) { String context = layersWithReferentFields . stream ( ) . map ( l -> l . name ( ) + "(" + l . schema ( ) . fields ( ) . entrySet ( ) . stream ( ) . filter ( e -> e . getValue ( ) . name ( ) == DataTypeName . SPAN && e . getValue ( ) . args ( ) . get ( "context" ) . stringValue ( ) . equals ( text . name ( ) ) ) . map ( Map . Entry :: getKey ) . collect ( Collectors . joining ( ", " ) ) + ")" ) . collect ( Collectors . joining ( ", " ) ) ; throw new DataInconsistencyException ( String . format ( "Removing text: %s results in data inconsistency, " + "because layers+fields { %s } are referring to this text, " + "remove these fields before removing text." , text . name ( ) , context ) ) ; } else if ( fieldcascade ) { for ( Layer layersWithReferentField : layersWithReferentFields ) { List < String > fields = layersWithReferentField . schema ( ) . fields ( ) . entrySet ( ) . stream ( ) . filter ( e -> e . getValue ( ) . name ( ) == DataTypeName . SPAN && e . getValue ( ) . args ( ) . get ( "context" ) . stringValue ( ) . equals ( text . name ( ) ) ) . map ( Map . Entry :: getKey ) . collect ( Collectors . toList ( ) ) ; fields . forEach ( layersWithReferentField :: removeField ) ; } } return this ; }
Remove text
 @param text         text to remove, (null is a no-op)
 @param fieldcascade remove fields in layers referring to this text, cascading removal
 @return this
Remove text @param text text to remove ( null is a no - op ) @param fieldcascade remove fields in layers referring to this text cascading removal @return this
//1. Verify no layer contains fields referencing this text.