001: package org.osbl.persistence.model;
002:
003: import org.osbl.persistence.model.Concrete;
004: import org.conform.Property;
005: import org.hibernate.Hibernate;
006:
007: import java.util.*;
008:
009: public abstract class General<C extends Concrete> extends Entity {
010: String qualifications;
011:
012: @Property(applicable="false")
013: List<C> concretes;
014:
015: public void setId(Long id) {
016: super .setId(id);
017: if (concretes != null) {
018: for (C c : concretes) {
019: c.id = id;
020: }
021: }
022: }
023:
024: public String getQualifications() {
025: return qualifications;
026: }
027:
028: public void setQualifications(String qualifications) {
029: this .qualifications = qualifications;
030: }
031:
032: public boolean hasQualification(String qualification) {
033: return qualifications != null
034: && qualifications.indexOf(qualification) != -1;
035: }
036:
037: public void addQualification(String qualification) {
038: if (qualifications == null)
039: qualifications = '|' + qualification;
040: else if (qualifications.indexOf(qualification) == -1)
041: qualifications += '|' + qualification;
042: }
043:
044: public void removeQualification(String qualification) {
045: if (qualifications != null) {
046: qualifications = qualifications.replaceFirst("\\|"
047: + qualification, "");
048: }
049: }
050:
051: public List<C> getConcretes() {
052: return concretes;
053: }
054:
055: public C getConcrete(Class clazz) {
056: if (concretes == null)
057: return null;
058:
059: for (C concrete : concretes) {
060: if (clazz.equals(Hibernate.getClass(concrete)))
061: return concrete;
062: }
063: return null;
064: }
065:
066: public String toString() {
067: return getClass().getSimpleName()
068: + (concretes != null ? concreteString() : "[]");
069: }
070:
071: private String concreteString() {
072: StringBuilder builder = new StringBuilder();
073: builder.append('[');
074: for (C c : concretes) {
075: if (builder.length() > 1)
076: builder.append(", ");
077: builder.append(c.getClass().getSimpleName());
078: }
079: builder.append(']');
080: return builder.toString();
081: }
082:
083: void removeConcrete(C concrete) {
084: if (concretes != null)
085: concretes.remove(concrete);
086: concrete.id = null;
087: }
088:
089: void addConcrete(C concrete) {
090: if (concretes == null)
091: concretes = new ArrayList<C>();
092: else {
093: for (Iterator<C> iterator = concretes.iterator(); iterator
094: .hasNext();) {
095: if (Hibernate.getClass(concrete).equals(
096: Hibernate.getClass(iterator.next()))) {
097: iterator.remove();
098: break;
099: }
100: }
101: }
102:
103: concretes.add(concrete);
104: concrete.id = getId();
105: }
106: }
|