Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevented duplicated tags #416

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/sql/migration/h2/h2-migration-from-v.2.1.0-to-v2.3.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ CREATE TABLE gs_tag (
color VARCHAR(255) NOT NULL,
description VARCHAR(255),
name VARCHAR(255) NOT NULL,
CONSTRAINT gs_tag_pkey PRIMARY KEY (id)
CONSTRAINT gs_tag_pkey PRIMARY KEY (id),
CONSTRAINT gs_tag_name_unique UNIQUE (name)
);

CREATE TABLE gs_resource_tags (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ create table gs_tag (
color varchar2(255 char) not null,
description varchar2(255 char),
"name" varchar2(255 char) not null,
primary key (id)
primary key (id),
constraint gs_tag_name_unique unique (name)
);

create table gs_resource_tags (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ create table gs_tag (
color varchar(255) not null,
description varchar(255) null,
"name" varchar(255) not null,
constraint gs_tag_pkey primary key (id)
constraint gs_tag_pkey primary key (id),
constraint gs_tag_name_unique unique (name)
);

create table gs_resource_tags (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class Tag implements Serializable {

@Id @GeneratedValue private Long id;

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

@Column(name = "color", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import it.geosolutions.geostore.core.model.Tag;
import it.geosolutions.geostore.services.exception.BadRequestServiceEx;
import it.geosolutions.geostore.services.exception.DuplicatedTagNameServiceException;
import it.geosolutions.geostore.services.exception.NotFoundServiceEx;
import java.util.List;

Expand All @@ -39,8 +40,9 @@ public interface TagService {
* @param tag
* @return long
* @throws BadRequestServiceEx
* @throws DuplicatedTagNameServiceException
*/
long insert(Tag tag) throws BadRequestServiceEx;
long insert(Tag tag) throws BadRequestServiceEx, DuplicatedTagNameServiceException;

/**
* @param page
Expand All @@ -63,8 +65,10 @@ public interface TagService {
* @return long
* @throws NotFoundServiceEx
* @throws BadRequestServiceEx
* @throws DuplicatedTagNameServiceException
*/
long update(long id, Tag tag) throws BadRequestServiceEx, NotFoundServiceEx;
long update(long id, Tag tag)
throws BadRequestServiceEx, NotFoundServiceEx, DuplicatedTagNameServiceException;

/**
* @param id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package it.geosolutions.geostore.services.exception;

public class DuplicatedTagNameServiceException extends GeoStoreServiceException {

public DuplicatedTagNameServiceException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import it.geosolutions.geostore.core.model.Resource;
import it.geosolutions.geostore.core.model.Tag;
import it.geosolutions.geostore.services.exception.BadRequestServiceEx;
import it.geosolutions.geostore.services.exception.DuplicatedTagNameServiceException;
import it.geosolutions.geostore.services.exception.NotFoundServiceEx;
import java.util.List;
import org.apache.logging.log4j.LogManager;
Expand All @@ -47,7 +48,7 @@ public void setResourceDAO(ResourceDAO resourceDAO) {
}

@Override
public long insert(Tag tag) throws BadRequestServiceEx {
public long insert(Tag tag) throws BadRequestServiceEx, DuplicatedTagNameServiceException {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Persisting Tag ... ");
}
Expand All @@ -56,6 +57,8 @@ public long insert(Tag tag) throws BadRequestServiceEx {
throw new BadRequestServiceEx("Tag must be specified");
}

checkForDuplicates(tag, false);

tagDAO.persist(tag);

return tag.getId();
Expand Down Expand Up @@ -90,12 +93,15 @@ public Tag get(long id) {
}

@Override
public long update(long id, Tag tag) throws BadRequestServiceEx, NotFoundServiceEx {
public long update(long id, Tag tag)
throws BadRequestServiceEx, NotFoundServiceEx, DuplicatedTagNameServiceException {
Tag original = get(id);
if (original == null) {
throw new NotFoundServiceEx("Tag not found");
}

checkForDuplicates(tag, true);

tag.setId(id);
tag.setResources(original.getResources());

Expand All @@ -104,6 +110,17 @@ public long update(long id, Tag tag) throws BadRequestServiceEx, NotFoundService
return id;
}

private void checkForDuplicates(Tag tag, boolean isUpdate) throws DuplicatedTagNameServiceException {
int duplicatesCount =
tagDAO.count(new Search().addFilterEqual("name", tag.getName()).setMaxResults(1));
if (duplicatesCount > 0) {
throw new DuplicatedTagNameServiceException(
String.format(
"Cannot %s the tag: a tag with the same name already exists.",
isUpdate ? "update" : "create"));
}
}

@Override
public void delete(long id) throws NotFoundServiceEx {
if (get(id) == null || !tagDAO.removeById(id)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import it.geosolutions.geostore.core.model.Resource;
import it.geosolutions.geostore.core.model.Tag;
import it.geosolutions.geostore.services.exception.BadRequestServiceEx;
import it.geosolutions.geostore.services.exception.DuplicatedTagNameServiceException;
import it.geosolutions.geostore.services.exception.NotFoundServiceEx;
import java.util.Collections;
import java.util.List;
Expand All @@ -49,6 +50,19 @@ public void testInsert() throws Exception {
assertTrue(foundTagsIds.stream().noneMatch(Objects::isNull));
}

public void testInsertDuplicate() throws Exception {

Tag tag = new Tag("tag-A", "#4561aa", "dusky");
Tag duplicateTag = new Tag(tag.getName(), "black", null);

tagService.insert(tag);

DuplicatedTagNameServiceException ex =
assertThrows(
DuplicatedTagNameServiceException.class, () -> tagService.insert(duplicateTag));
assertTrue(ex.getMessage().contains("create"));
}

public void testInsertNull() throws Exception {
assertThrows(BadRequestServiceEx.class, () -> tagService.insert(null));
}
Expand Down Expand Up @@ -114,6 +128,22 @@ public void testUpdate() throws Exception {
assertEquals(expected_tag, updatedTag);
}

public void testUpdateWithDuplicate() throws Exception {

Tag tagA = new Tag("tag-A", "#4561aa", "dusky");
Tag tagB = new Tag("tag-B", "black", null);

tagDAO.persist(tagA, tagB);

tagB.setName(tagA.getName());

DuplicatedTagNameServiceException ex =
assertThrows(
DuplicatedTagNameServiceException.class,
() -> tagService.update(tagB.getId(), tagB));
assertTrue(ex.getMessage().contains("update"));
}

public void testUpdateWithResource() throws Exception {

Tag update_tag = new Tag("updated name", "black", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import it.geosolutions.geostore.core.model.Tag;
import it.geosolutions.geostore.services.TagService;
import it.geosolutions.geostore.services.exception.BadRequestServiceEx;
import it.geosolutions.geostore.services.exception.DuplicatedTagNameServiceException;
import it.geosolutions.geostore.services.exception.NotFoundServiceEx;
import it.geosolutions.geostore.services.rest.RESTTagService;
import it.geosolutions.geostore.services.rest.exception.BadRequestWebEx;
import it.geosolutions.geostore.services.rest.exception.ConflictWebEx;
import it.geosolutions.geostore.services.rest.exception.NotFoundWebEx;
import it.geosolutions.geostore.services.rest.model.TagList;
import java.util.List;
Expand Down Expand Up @@ -59,6 +61,8 @@ public long insert(SecurityContext sc, Tag tag) {
} catch (BadRequestServiceEx e) {
LOGGER.error(e.getMessage(), e);
throw new BadRequestWebEx(e.getMessage());
} catch (DuplicatedTagNameServiceException e) {
throw new ConflictWebEx(e.getMessage());
}
}

Expand Down Expand Up @@ -101,6 +105,8 @@ public long update(SecurityContext sc, long id, Tag tag) {
throw new BadRequestWebEx(e.getMessage());
} catch (NotFoundServiceEx e) {
throw new NotFoundWebEx(e.getMessage());
} catch (DuplicatedTagNameServiceException e) {
throw new ConflictWebEx(e.getMessage());
}
}

Expand Down