All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.knightsoftnet.gwtp.spring.shared.db.LocalizedEntity Maven / Gradle / Ivy

There is a newer version: 2.2.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You 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.knightsoftnet.gwtp.spring.shared.db;

import de.knightsoftnet.gwtp.spring.shared.data.jpa.domain.AbstractPersistable;
import de.knightsoftnet.validators.shared.interfaces.LocalizedValue;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotEmpty;

@Entity
@Table(name = "localized")
public class LocalizedEntity extends AbstractPersistable
    implements LocalizedValue, Serializable {

  private static final long serialVersionUID = 5479449217468715495L;

  @ElementCollection(fetch = FetchType.EAGER)
  private Map<@NotEmpty String, String> localizedText;

  public LocalizedEntity() {
    this(null, new HashMap<>());
  }

  public LocalizedEntity(final Long id) {
    this(id, new HashMap<>());
  }

  public LocalizedEntity(final Map map) {
    this(null, map);
  }

  /**
   * constructor initialize id and localized map.
   *
   * @param id entity id of the entry
   * @param map the map with the localized entries
   */
  public LocalizedEntity(final Long id, final Map map) {
    super();
    setId(id);
    setLocalizedText(map); // NOPMD when final hybernate fails
  }

  @Override
  public void setLocalizedText(final Map plocalizedText) {
    if (plocalizedText == null) {
      if (localizedText == null) {
        localizedText = new HashMap<>();
      } else {
        localizedText.clear();
      }
    } else {
      localizedText = plocalizedText;
    }
  }

  @Override
  public void putLocalizedText(final String plocale, final String ptext) {
    localizedText.put(plocale, ptext);
  }

  @Override
  public Map getLocalizedText() {
    return localizedText;
  }

  @Override
  public String getLocalizedText(final String plocale) {
    return localizedText.get(plocale);
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(getId());
  }

  @Override
  public boolean equals(final Object obj) {

    if (null == obj) {
      return false;
    }

    if (this == obj) {
      return true;
    }

    if (getClass() != obj.getClass()) {
      return false;
    }

    final LocalizedEntity that = (LocalizedEntity) obj;

    return Objects.equals(getId(), that.getId())
        && Objects.equals(getLocalizedText(), that.getLocalizedText());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy