TranslationService.java
/*
* Copyright 2021 Global Crop Diversity Trust
*
* 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 org.gringlobal.service;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import org.gringlobal.model.CooperatorOwnedLang;
import org.gringlobal.model.SysLang;
import org.gringlobal.model.TranslatedCooperatorOwnedModel;
import org.gringlobal.service.filter.TranslatedEntityFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
/**
* The Interface TranslationService.
*
* @param <E> the Entity type
* @param <L> the EntityLang type
* @param <T> the TranslatedEntity type
* @param <F> the EntityFilter type
*
* @author Maxym Borodenko
*/
@Transactional(readOnly = true)
public interface TranslationService<
// The entity
E extends TranslatedCooperatorOwnedModel<L, E>,
// The entityLang
L extends CooperatorOwnedLang<L, E>,
// Translated entity
T extends TranslationService.Translation<E, L>,
// Translated entity filter
F extends TranslatedEntityFilter<?, ?>
> extends CRUDService<L> {
/**
* List.
*
* @param filter the filter
* @param page the page
* @return the page
*/
Page<T> list(F filter, Pageable page);
/**
* Gets the translated.
*
* @param entity the entity
* @return the translated
*/
T getTranslated(E entity);
/**
* Gets the translated list.
*
* @param entityList the list entities
* @return the translated list
*/
List<T> getTranslated(List<E> entityList);
/**
* The Class Translation.
*
* @param <E> the element type
* @param <L> the generic type
*/
@Setter
@Getter
abstract class Translation<E extends TranslatedCooperatorOwnedModel<L, E>, L extends CooperatorOwnedLang<L, E>> implements Serializable {
private static final long serialVersionUID = 7629603657547628388L;
/** The entity. */
@JsonUnwrapped
@NotNull
@JsonIgnoreProperties("id")
public E entity;
/** The title. */
@NotNull
@Size(min = 1, max = 500)
public String title;
/** The description. */
public String description;
/**
* Instantiates a new translation.
*/
protected Translation() {
}
/**
* Instantiates a new translation.
*
* @param entity the entity
* @param title the title
* @param description the description
*/
public Translation(E entity, String title, String description) {
this.entity = entity;
this.title = title;
this.description = description;
}
/**
* Primarily here for JSON serialization and @JsonIdentityInfo annotation.
*
* @return original entity ID
*/
@JsonProperty("id")
public Long getId() {
return entity.getId();
}
/**
* Needed for successful JSON de-serialization.
*
* @param id
*/
public void setId(Long id) {
// Apparently this does not hurt, but must be declared for JSON de-serialization.
// System.err.println("setId " + id);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Translation<?, ?> that = (Translation<?, ?>) o;
if (!Objects.equals(entity, that.entity)) return false;
if (!Objects.equals(title, that.title)) return false;
return Objects.equals(description, that.description);
}
@Override
public int hashCode() {
int result = entity != null ? entity.hashCode() : 0;
result = 31 * result + (title != null ? title.hashCode() : 0);
result = 31 * result + (description != null ? description.hashCode() : 0);
return result;
}
/**
* Update target with L data.
*
* @param target the target
* @return the l
*/
public L updateLang(L target) {
target.setTitle(title);
target.setDescription(description);
target.setEntity(entity);
return target;
}
}
List<L> listTranslations(E e);
L deleteTranslation(E entity, SysLang sysLang);
L addLang(E reloaded, SysLang sysLang, L input);
L upsertLang(E reloaded, SysLang sysLang, L input);
L newLang();
L getLang(@NotNull E entity, SysLang sysLang);
}