<commit_before>/*
 * Copyright 2020
 * Ubiquitous Knowledge Processing (UKP) Lab and FG Language Technology
 * Technische Universität Darmstadt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.tudarmstadt.ukp.clarin.webanno.ui.annotation.detail;

import static de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.SPAN_TYPE;
import static de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectAnnotationByAddr;
import static de.tudarmstadt.ukp.clarin.webanno.support.lambda.LambdaBehavior.visibleWhen;
import static java.util.Arrays.asList;
import static org.apache.commons.lang3.StringUtils.isEmpty;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.uima.cas.CAS;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.wicket.ClassAttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.RefreshingView;
import org.apache.wicket.markup.repeater.util.ModelIteratorAdapter;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService;
import de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation;
import de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation.Direction;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.action.AnnotationActionHandler;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.layer.LayerSupportRegistry;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.page.AnnotationPageBase;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.Renderer;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil;
import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature;
import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer;
import de.tudarmstadt.ukp.clarin.webanno.support.lambda.LambdaAjaxLink;

public class AttachedAnnotationListPanel
    extends Panel
{
    private static final long serialVersionUID = 2229431469432051779L;

    private static final Logger LOG = LoggerFactory.getLogger(AttachedAnnotationListPanel.class);

    private @SpringBean AnnotationSchemaService schemaService;
    private @SpringBean LayerSupportRegistry layerRegistry;

    private final AnnotationPageBase page;
    private final WebMarkupContainer noAttachedAnnotationsInfo;
    private final WebMarkupContainer attachedAnnotationsContainer;
    private final AnnotationActionHandler actionHandler;

    private final IModel<List<AttachedAnnotationInfo>> annotations;

    public AttachedAnnotationListPanel(String aId, AnnotationPageBase aPage,
            AnnotationActionHandler aActionHandler, IModel<AnnotatorState> aModel)
    {
        super(aId, aModel);

        page = aPage;
        annotations = LoadableDetachableModel.of(this::getRelationInfo);
        actionHandler = aActionHandler;

        noAttachedAnnotationsInfo = new WebMarkupContainer("noAttachedAnnotationsInfo");
        noAttachedAnnotationsInfo.setOutputMarkupPlaceholderTag(true);
        noAttachedAnnotationsInfo.add(visibleWhen(
                () -> getModelObject() != null && getModelObject().getSelection().isSet()
                        && SPAN_TYPE.equals(getModelObject().getSelectedAnnotationLayer().getType())
                        && annotations.getObject().isEmpty()));
        add(noAttachedAnnotationsInfo);

        attachedAnnotationsContainer = new WebMarkupContainer("attachedAnnotationsContainer");
        attachedAnnotationsContainer.setOutputMarkupPlaceholderTag(true);
        attachedAnnotationsContainer.add(new AttachedAnnotationList("annotations", annotations));
        attachedAnnotationsContainer.add(visibleWhen(() -> !annotations.getObject().isEmpty()));
        add(attachedAnnotationsContainer);
    }

    @SuppressWarnings("unchecked")
    public IModel<AnnotatorState> getModel()
    {
        return (IModel<AnnotatorState>) getDefaultModel();
    }

    public AnnotatorState getModelObject()
    {
        return (AnnotatorState) getDefaultModelObject();
    }

    private List<AttachedAnnotationInfo> getRelationInfo()
    {
        Selection selection = getModelObject().getSelection();

        if (!selection.isSet()) {
            return Collections.emptyList();
        }

        CAS cas;
        try {
            cas = page.getEditorCas();
        }
        catch (IOException e) {
            // If we have trouble accessing the CAS, we probably never get here anyway...
            // the AnnotationPageBase should already have found the issue and displayed some
            // error to the user.
            LOG.error("Unable to access editor CAS", e);
            return Collections.emptyList();
        }

        AnnotationFS annoFs = selectAnnotationByAddr(cas, selection.getAnnotation().getId());
        VID localVid = new VID(annoFs);<commit_msg>No issue: Fix problem when reloading the browser while viewing another users annotations who has just made a change to their annotations.<commit_after>/*
 * Copyright 2020
 * Ubiquitous Knowledge Processing (UKP) Lab and FG Language Technology
 * Technische Universität Darmstadt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.tudarmstadt.ukp.clarin.webanno.ui.annotation.detail;

import static de.tudarmstadt.ukp.clarin.webanno.api.WebAnnoConst.SPAN_TYPE;
import static de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID.NONE;
import static de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.WebAnnoCasUtil.selectAnnotationByAddr;
import static de.tudarmstadt.ukp.clarin.webanno.support.lambda.LambdaBehavior.visibleWhen;
import static java.util.Arrays.asList;
import static org.apache.commons.lang3.StringUtils.isEmpty;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.uima.cas.CAS;
import org.apache.uima.cas.text.AnnotationFS;
import org.apache.wicket.ClassAttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.RefreshingView;
import org.apache.wicket.markup.repeater.util.ModelIteratorAdapter;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.tudarmstadt.ukp.clarin.webanno.api.AnnotationSchemaService;
import de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation;
import de.tudarmstadt.ukp.clarin.webanno.api.AttachedAnnotation.Direction;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.action.AnnotationActionHandler;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.adapter.TypeAdapter;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.layer.LayerSupportRegistry;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.AnnotatorState;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.Selection;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.model.VID;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.page.AnnotationPageBase;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.rendering.Renderer;
import de.tudarmstadt.ukp.clarin.webanno.api.annotation.util.TypeUtil;
import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationFeature;
import de.tudarmstadt.ukp.clarin.webanno.model.AnnotationLayer;
import de.tudarmstadt.ukp.clarin.webanno.support.lambda.LambdaAjaxLink;

public class AttachedAnnotationListPanel
    extends Panel
{
    private static final long serialVersionUID = 2229431469432051779L;

    private static final Logger LOG = LoggerFactory.getLogger(AttachedAnnotationListPanel.class);

    private @SpringBean AnnotationSchemaService schemaService;
    private @SpringBean LayerSupportRegistry layerRegistry;

    private final AnnotationPageBase page;
    private final WebMarkupContainer noAttachedAnnotationsInfo;
    private final WebMarkupContainer attachedAnnotationsContainer;
    private final AnnotationActionHandler actionHandler;

    private final IModel<List<AttachedAnnotationInfo>> annotations;

    public AttachedAnnotationListPanel(String aId, AnnotationPageBase aPage,
            AnnotationActionHandler aActionHandler, IModel<AnnotatorState> aModel)
    {
        super(aId, aModel);

        page = aPage;
        annotations = LoadableDetachableModel.of(this::getRelationInfo);
        actionHandler = aActionHandler;

        noAttachedAnnotationsInfo = new WebMarkupContainer("noAttachedAnnotationsInfo");
        noAttachedAnnotationsInfo.setOutputMarkupPlaceholderTag(true);
        noAttachedAnnotationsInfo.add(visibleWhen(
                () -> getModelObject() != null && getModelObject().getSelection().isSet()
                        && getModelObject().getSelection().getAnnotation().getId() != NONE
                        && SPAN_TYPE.equals(getModelObject().getSelectedAnnotationLayer().getType())
                        && annotations.getObject().isEmpty()));
        add(noAttachedAnnotationsInfo);

        attachedAnnotationsContainer = new WebMarkupContainer("attachedAnnotationsContainer");
        attachedAnnotationsContainer.setOutputMarkupPlaceholderTag(true);
        attachedAnnotationsContainer.add(new AttachedAnnotationList("annotations", annotations));
        attachedAnnotationsContainer.add(visibleWhen(() -> !annotations.getObject().isEmpty()));
        add(attachedAnnotationsContainer);
    }

    @SuppressWarnings("unchecked")
    public IModel<AnnotatorState> getModel()
    {
        return (IModel<AnnotatorState>) getDefaultModel();
    }

    public AnnotatorState getModelObject()
    {
        return (AnnotatorState) getDefaultModelObject();
    }

    private List<AttachedAnnotationInfo> getRelationInfo()
    {
        Selection selection = getModelObject().getSelection();

        if (!selection.isSet() || selection.getAnnotation().getId() == NONE) {
            return Collections.emptyList();
        }

        CAS cas;
        try {
            cas = page.getEditorCas();
        }
        catch (IOException e) {
            // If we have trouble accessing the CAS, we probably never get here anyway...
            // the AnnotationPageBase should already have found the issue and displayed some
            // error to the user.
            LOG.error("Unable to access editor CAS", e);
            return Collections.emptyList();
        }

        AnnotationFS annoFs = selectAnnotationByAddr(cas, selection.getAnnotation().getId());
        VID localVid = new VID(annoFs);
{'fraction_non_alphanumeric': 0.09281602397287281, 'fraction_numerical': 0.0047314880529926665, 'mean_word_length': 4.279766860949209, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1226149', 'n_tokens_mistral': 3674, 'n_tokens_neox': 3593, 'n_words': 719}