JPA migration: Some more entities

git-svn-id: https://svn.libreccm.org/ccm/jpa@3364 8810af33-2d31-482b-a856-94f89814c4df
pull/2/head
jensp 2015-04-29 17:47:05 +00:00
parent a749e9d63b
commit 4e89a8a1a0
6 changed files with 576 additions and 12 deletions

View File

@ -0,0 +1,38 @@
/*
* Copyright (C) 2015 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.categorization;
import org.libreccm.core.CcmObject;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Table;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@Entity
@Table(name = "categories")
public class Category extends CcmObject implements Serializable {
private static final long serialVersionUID = -7250208963391878547L;
}

View File

@ -0,0 +1,210 @@
/*
* Copyright (C) 2015 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.categorization;
import org.libreccm.core.CcmObject;
import org.libreccm.l10n.LocalizedString;
import java.io.Serializable;
import java.net.URI;
import java.util.Date;
import java.util.Objects;
import javax.persistence.AssociationOverride;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@Entity
@Table(name = "domains")
public class Domain extends CcmObject implements Serializable {
private static final long serialVersionUID = 4012590760598188732L;
@Column(name = "domain_key", nullable = false, unique = true)
private String domainKey;
@Column(name = "uri", nullable = false, unique = true)
private URI uri;
@Embedded
@AssociationOverride(
name = "values",
joinTable = @JoinTable(name = "category_titles",
joinColumns = {
@JoinColumn(name = "object_id")}))
private LocalizedString title;
@Embedded
@AssociationOverride(
name = "values",
joinTable = @JoinTable(name = "category_descriptions",
joinColumns = {
@JoinColumn(name = "object_id")}))
private LocalizedString description;
@Column(name = "version", nullable = false)
private String version;
@Column(name = "released")
@Temporal(TemporalType.TIMESTAMP)
private Date released;
@ManyToOne
private Category root;
public String getDomainKey() {
return domainKey;
}
public void setDomainKey(final String domainKey) {
this.domainKey = domainKey;
}
public URI getUri() {
return uri;
}
public void setUri(final URI uri) {
this.uri = uri;
}
public LocalizedString getTitle() {
return title;
}
public void setTitle(final LocalizedString title) {
this.title = title;
}
public LocalizedString getDescription() {
return description;
}
public void setDescription(final LocalizedString description) {
this.description = description;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public Date getReleased() {
return new Date(released.getTime());
}
public void setReleased(final Date released) {
this.released = new Date(released.getTime());
}
public Category getRoot() {
return root;
}
protected void setRoot(final Category root) {
this.root = root;
}
@Override
public int hashCode() {
int hash = 3;
hash = 41 * hash + Objects.hashCode(domainKey);
hash = 41 * hash + Objects.hashCode(uri);
hash = 41 * hash + Objects.hashCode(title);
hash = 41 * hash + Objects.hashCode(description);
hash = 41 * hash + Objects.hashCode(version);
hash = 41 * hash + Objects.hashCode(released);
hash = 41 * hash + Objects.hashCode(root);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Domain other = (Domain) obj;
if (!other.canEqual(this)) {
return false;
}
if (!Objects.equals(domainKey, other.getDomainKey())) {
return false;
}
if (!Objects.equals(uri, other.getUri())) {
return false;
}
if (!Objects.equals(title, other.getTitle())) {
return false;
}
if (!Objects.equals(description, other.getDescription())) {
return false;
}
if (!Objects.equals(version, other.getVersion())) {
return false;
}
if (!Objects.equals(released, other.getReleased())) {
return false;
}
return Objects.equals(root, other.getRoot());
}
@Override
public boolean canEqual(final Object obj) {
return obj instanceof Domain;
}
@Override
public String toString(final String data) {
return String.format(
", domainKey = \"%s\", "
+ "uri = \"%s\", "
+ "title = \"%s\", "
+ "version = \"%s\", "
+ "released = %tF %<tT, "
+ "root = \"%s\"%s",
domainKey,
uri.toString(),
title.toString(),
version,
released,
root.toString(),
data
);
}
}

View File

@ -54,7 +54,7 @@ public class CcmObject implements Serializable {
return objectId; return objectId;
} }
public void setObjectId(long objectId) { public void setObjectId(final long objectId) {
this.objectId = objectId; this.objectId = objectId;
} }
@ -62,7 +62,7 @@ public class CcmObject implements Serializable {
return displayName; return displayName;
} }
public void setDisplayName(String displayName) { public void setDisplayName(final String displayName) {
this.displayName = displayName; this.displayName = displayName;
} }

View File

@ -18,10 +18,25 @@
*/ */
package org.libreccm.core; package org.libreccm.core;
import java.io.Serializable; import org.libreccm.l10n.LocalizedString;
import java.io.Serializable;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import javax.persistence.AssociationOverride;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/** /**
* *
@ -31,17 +46,142 @@ import javax.persistence.Table;
@Table(name = "resources") @Table(name = "resources")
public class Resource extends CcmObject implements Serializable { public class Resource extends CcmObject implements Serializable {
private static final long serialVersionUID = 201502291330L; private static final long serialVersionUID = 7345482620613842781L;
private long resourceId; @Embedded
@AssociationOverride(
name = "values",
joinTable = @JoinTable(name = "resource_titles",
joinColumns = {
@JoinColumn(name = "object_id")}))
private LocalizedString title;
public long getResourceId() { @Embedded
return resourceId; @AssociationOverride(
name = "values",
joinTable = @JoinTable(name = "resource_descriptions",
joinColumns = {
@JoinColumn(name = "object_id")}))
private LocalizedString description;
@Column(name = "created")
@Temporal(TemporalType.TIMESTAMP)
private Date created;
@OneToMany(mappedBy = "parent")
private List<Resource> childs;
@ManyToOne
private Resource parent;
// @Column(name = "resource_type")
// private String resourceType;
public LocalizedString getTitle() {
return title;
} }
public void setResourceId(long resourceId) { public void setTitle(LocalizedString title) {
this.resourceId = resourceId; this.title = title;
} }
public LocalizedString getDescription() {
return description;
}
public void setDescription(LocalizedString description) {
this.description = description;
}
public Date getCreated() {
return new Date(created.getTime());
}
public void setCreated(final Date created) {
this.created = new Date(created.getTime());
}
public List<Resource> getChilds() {
return Collections.unmodifiableList(childs);
}
protected void setChilds(final List<Resource> childs) {
this.childs = childs;
}
protected void addChild(final Resource child) {
childs.add(child);
}
protected void removeChild(final Resource child) {
childs.remove(child);
}
public Resource getParent() {
return parent;
}
protected void setParent(Resource parent) {
this.parent = parent;
}
// public String getResourceType() {
// return resourceType;
// }
//
// public void setResourceType(final String resourceType) {
// this.resourceType = resourceType;
// }
@Override
public int hashCode() {
int hash = 5;
hash = 29 * hash + Objects.hashCode(title);
hash = 29 * hash + Objects.hashCode(description);
hash = 29 * hash + Objects.hashCode(created);
hash = 29 * hash + Objects.hashCode(parent);
return hash;
}
@Override
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Resource other = (Resource) obj;
if (!other.canEqual(this)) {
return false;
}
if (!Objects.equals(title, other.getTitle())) {
return false;
}
if (!Objects.equals(description, other.getDescription())) {
return false;
}
if (!Objects.equals(created, other.getCreated())) {
return false;
}
return Objects.equals(parent, other.getParent());
}
@Override
public boolean canEqual(final Object obj) {
return obj instanceof Resource;
}
@Override
public String toString(final String data) {
return super.toString(
String.format(", title = %s, "
+ "created = %tF %<tT%s",
title.toString(),
created,
data));
}
} }

View File

@ -0,0 +1,171 @@
/*
* Copyright (C) 2015 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.l10n;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embeddable;
import javax.persistence.Lob;
import javax.persistence.MapKeyColumn;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
/**
* A helper class for localisable string properties. This class is declared as
* embeddable, so that it can be used in every other entity. The localised
* values are stored in a {@link Map}. This class is <em>not</em> designed to be
* overwritten. But because it is an entity class we can't make the class final.
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@Embeddable
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class LocalizedString implements Serializable {
private static final long serialVersionUID = 7378282657084330425L;
/**
* The localised values of the string.
*/
@ElementCollection
@MapKeyColumn(name = "locale")
@Column(name = "value")
@Lob
@XmlElementWrapper(name = "values")
@XmlElement(name = "value")
private Map<Locale, String> values;
/**
* Constructor. Only creates the initial, empty map for new instances.
*/
public LocalizedString() {
values = new HashMap<>();
}
/**
* Get all localised values.
*
* @return A unmodifiable {@code Map} containing all localised values of
* this localised string.
*/
public Map<Locale, String> getValues() {
if (values == null) {
return null;
} else {
return Collections.unmodifiableMap(values);
}
}
/**
* Setter for replacing the complete {@code Map} of values. Only to be used
* by JPA and the Repository classes in the package.
*
* @param values The new map of values.
*/
protected void setValues(final Map<Locale, String> values) {
this.values = values;
}
/**
* Retrieves the values for the default locale.
*
* @return The localised value for the default locale of the system the
* application is running on. In most cases this is not what you
* want. Use {@link #getValue(java.util.Locale)} instead.
*/
public String getValue() {
return getValue(Locale.getDefault());
}
/**
* Retrieves the localised value of a locale.
*
* @param locale The locale for which the value shall be retrieved.
*
* @return The localised for the {@code locale} or {@code null} if there is
* no value for the provided locale.
*/
public String getValue(final Locale locale) {
return values.get(locale);
}
/**
* Add a new localised value for a locale. If there is already a value for
* the provided locale the value is replaced with the new value.
*
* @param locale The locale of the provided value.
* @param value The localised value for the provided locale.
*/
public void addValue(final Locale locale, final String value) {
values.put(locale, value);
}
/**
* Removes the value for the provided locale.
*
* @param locale The locale for which the value shall be removed.
*/
public void removeValue(final Locale locale) {
values.remove(locale);
}
/**
* Checks if a localised string instance has a value for a locale.
*
* @param locale The locale.
*
* @return {@code true} if this localised string has a value for the
* provided locale, {@code false} if not.
*/
public boolean hasValue(final Locale locale) {
return values.containsKey(locale);
}
/**
* Retrieves all present locales.
*
* @return A {@link Set} containing all locales for which this localised
* string has values.
*/
public Set<Locale> getAvailableLocales() {
return values.keySet();
}
@Override
public String toString() {
return String.format(
"%s{ "
+ "%s"
+ " }",
super.toString(),
values.toString());
}
}

View File

@ -149,6 +149,11 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>de.jpdigital</groupId>
<artifactId>hibernate4-ddl-maven-plugin</artifactId>
<version>1.0.0-alpha.1</version>
</plugin>
</plugins> </plugins>
</pluginManagement> </pluginManagement>
</build> </build>