Skip to content

Commit

Permalink
#2824 - Layer name with special characters causes problems in search
Browse files Browse the repository at this point in the history
- Start introducing a "shortName" field in the layer which could be used to configure a name that would be compatible with the index
- Change DB schema
- Started changing layer detail form
- Other stuff still missing
  • Loading branch information
reckart committed Sep 7, 2024
1 parent a54bcbe commit bc0c2d9
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
*/
package de.tudarmstadt.ukp.clarin.webanno.model;

import static com.nimbusds.oauth2.sdk.util.StringUtils.isBlank;
import static jakarta.persistence.ConstraintMode.NO_CONSTRAINT;
import static java.util.Arrays.asList;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;
import org.apache.uima.jcas.cas.TOP;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
Expand Down Expand Up @@ -63,9 +65,15 @@ public class AnnotationLayer
@Column(name = "id")
private Long id;

@Column(name = "name", nullable = false)
private String name;

@Column(nullable = false)
private String uiName;

@Column(name = "short_name", nullable = true)
private String shortName;

@Column(nullable = false)
private String type;

Expand All @@ -82,9 +90,6 @@ public class AnnotationLayer
@Column(length = 64000)
private String onClickJavascriptAction;

@Column(name = "name", nullable = false)
private String name;

@ManyToOne
// @ForeignKey(ConstraintMode.NO_CONSTRAINT)
@JoinColumn(name = "annotation_type", //
Expand Down Expand Up @@ -148,6 +153,7 @@ private AnnotationLayer(Builder builder)
{
this.id = builder.id;
this.uiName = builder.uiName;
this.shortName = builder.shortName;
this.type = builder.type;
this.description = builder.description;
this.enabled = builder.enabled;
Expand Down Expand Up @@ -223,6 +229,29 @@ public void setDescription(String aDescription)
description = aDescription;
}

/**
* The name under which the layer should be accessible when searching
*
* @param aShortName
*/
public void setShortName(String aShortName)
{
shortName = aShortName;
}

public String getShortName()
{
if (isBlank(shortName)) {
if (name != null && name.contains(".")) {
return StringUtils.substringAfterLast(name, ".");
}

return name;
}

return shortName;
}

/**
* The name displayed to the user in the UI.
*
Expand Down Expand Up @@ -573,14 +602,15 @@ public static Builder builder()
public static final class Builder
{
private Long id;
private String name;
private String uiName;
private String shortName;
private String type;
private String description;
private boolean enabled = true;
private boolean builtIn = false;
private boolean readonly = false;
private String onClickJavascriptAction;
private String name;
private AnnotationLayer attachType;
private AnnotationFeature attachFeature;
private Project project;
Expand Down Expand Up @@ -623,6 +653,12 @@ public Builder withUiName(String uiName)
return this;
}

public Builder withShortName(String aSearchName)
{
shortName = aSearchName;
return this;
}

public Builder withType(String type)
{
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1394,4 +1394,15 @@
<changeSet author="INCEpTION Team" id="20240629-14-pg" dbms="postgresql">
<sql>ALTER TABLE annotation_feature ALTER "curatable" TYPE BOOLEAN USING ("curatable"::int::bool)</sql>
</changeSet>

<changeSet author="INCEpTION Team" id="20240907-01">
<preConditions onFail="MARK_RAN">
<not>
<columnExists tableName="annotation_type" columnName="short_name" />
</not>
</preConditions>
<addColumn tableName="annotation_type">
<column name="short_name" type="VARCHAR(255)"/>
</addColumn>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ public interface AnnotationSchemaService
*/
boolean existsLayer(String name, Project project);

/**
* Check if an {@link AnnotationLayer} exists with this short name in the given {@link Project}.
*
* @param shortName
* the layer name.
* @param project
* the project.
* @return if the layer exists.
*/
boolean existsLayerWithShortName(String shortName, Project project);

/**
* check if an {@link AnnotationLayer} exists with this name and type in this {@link Project}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,23 @@ public boolean existsLayer(String aName, Project aProject)
}
}

@Override
@Transactional(noRollbackFor = NoResultException.class)
public boolean existsLayerWithShortName(String aShortName, Project aProject)
{
try {
entityManager.createQuery(
"FROM AnnotationLayer WHERE shortName = :shortName AND project = :project",
AnnotationLayer.class).setParameter("shortName", aShortName) //
.setParameter("project", aProject) //
.getSingleResult();
return true;
}
catch (NoResultException e) {
return false;
}
}

@Override
@Transactional(noRollbackFor = NoResultException.class)
public boolean existsLayer(String aName, String aType, Project aProject)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ enabled=Enabled

id=ID
name=Name
shortName=Short name
slug=URL slug
description=Description
format=Format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public class LayerDetailForm
private DropDownChoice<AnnotationLayer> attachTypeSelect;
private Label effectiveAttachType;
private TextField<String> uiName;
private TextField<String> shortName;

private FeatureSelectionForm featureSelectionForm;
private FeatureDetailForm featureDetailForm;
Expand Down Expand Up @@ -129,6 +130,11 @@ public LayerDetailForm(String id, IModel<AnnotationLayer> aSelectedLayer,
uiName.setOutputMarkupId(true);
uiName.setRequired(true);
add(uiName);

shortName = new TextField<String>("shortName");
shortName.setOutputMarkupId(true);
add(shortName);

add(new TextArea<String>("description").setOutputMarkupPlaceholderTag(true));

add(new Label("name").add(visibleWhen(() -> isNotBlank(getModelObject().getName()))));
Expand Down Expand Up @@ -373,6 +379,13 @@ private void actionSave(AjaxRequestTarget aTarget, Form<AnnotationLayer> aForm)
return;
}

if (annotationService.existsLayerWithShortName(shortLayerName, project)) {
error("A layer with the short name [" + shortLayerName
+ "] already exists in this project.");
return;
}

layer.setShortName(shortLayerName);
layer.setName(layerName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@
<a wicket:id="technicalPropertiesHelpLink"/>
</div>
<div class="card-body">
<div class="form-row form-floating">
<input wicket:id="shortName" class="form-control" wicket:message="placeholder:shortName"/>
<label wicket:for="shortName">
<wicket:label key="shortName"/>
</label>
</div>
<div class="form-row form-floating" wicket:enclosure="name">
<span wicket:id="name" class="form-control" readonly/>
<label>
Expand Down

0 comments on commit bc0c2d9

Please sign in to comment.