Skip to content

Instantly share code, notes, and snippets.

@wombat
Last active December 11, 2015 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wombat/4538159 to your computer and use it in GitHub Desktop.
Save wombat/4538159 to your computer and use it in GitHub Desktop.
Dynamic product description translations
@XmlRootElement
@Entity
@Table(name = "sc_table")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "sc_table_type", discriminatorType = DiscriminatorType.STRING)
public class ScTable extends ScModel implements Serializable {
@Getter
@Setter
@Column(name = "code")
private String code;
@Getter
@Setter
@OneToMany(mappedBy = "scTable", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Translation> translations = new ArrayList<Translation>();
@Transient
private String description = "";
public String getDescription() {
return getDescription("it");
}
public void setDescription(String value) {
setDescription("it", value);
}
public String getDescription(String languageCode) {
String result = "";
Translation translation = null;
for (Translation localeTranslation : translations) {
if (localeTranslation.getLanguageCode().equalsIgnoreCase(
languageCode)) {
translation = localeTranslation;
break;
}
}
if (translation != null) {
result = translation.getDescription();
}
return result;
}
public void setDescription(String languageCode, String value) {
boolean localeUpdated = false;
if (translations == null) {
translations = new ArrayList<Translation>();
}
for (Translation translation : translations) {
if (translation.getLanguageCode().equals(languageCode)) {
localeUpdated = true;
translation.setDescription(value);
break;
}
}
if (!localeUpdated) {
Translation ct = new Translation();
ct.setLanguageCode(languageCode);
ct.setDescription(value);
ct.setScTable(this);
translations.add(ct);
}
}
}
@XmlRootElement
@Entity
@Table(name = "size")
@DiscriminatorValue(value = "size")
public class Size extends ScTable implements Serializable {
@Getter
@Setter
private String size = "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment