Package dnb.data.impl

Source Code of dnb.data.impl.LabelHibernateImpl

package dnb.data.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import dnb.data.Label;
import dnb.data.Labelcode;
import dnb.util.InsertionSort;
import dnb.util.MatchUtils;
import dnb.util.MatchUtils.MatchUtilsResult;


@Entity(name= "label"public class LabelHibernateImpl extends RepositoryObjectHibernateImpl implements Label {
   
  private static final long serialVersionUID = 8330750418348979547L;

  @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
  @JoinColumn(name="LABEL_ID")
  private List<LabelcodeHibernateImpl> codes = new ArrayList<LabelcodeHibernateImpl>();
   
  private transient RepositoryHibernateImpl parent;
  /** Indexing: when the label name contains > 1 words*/
  private transient String[] parts;
  private transient String normalizedName;
  public LabelHibernateImpl() {}
 
  public LabelHibernateImpl(RepositoryHibernateImpl parent, String name) {   
    super(name);
    init(name);
    this.parent = parent;
  }
 
  void postLoad(RepositoryHibernateImpl parent) {
    this.parent = parent;
    init(getName());
  }
 
  private void init(String name) {
    MatchUtilsResult mur = MatchUtils.getPartsNormalize(name);
    parts = mur.getParts();
    normalizedName = mur.getNormalized();
  }
 
  public Labelcode findLabelcode(String labelcode) {
    for (LabelcodeHibernateImpl lc : codes) {
      if (lc.getName().equalsIgnoreCase(labelcode)) {
        return lc; // already contained
      }
    }
    return null;
  }
 
  /* (non-Javadoc)
   * @see dnb.data.Label#addCode(java.lang.String)
   */
  @SuppressWarnings("unchecked")
  public void addCode(String code) {
    if (code == null) {
      throw new IllegalArgumentException("Cannot add null code.");
    }
    // if lc already exists, do nothing   
    for (LabelcodeHibernateImpl c : codes) {
      if (c.getName().equalsIgnoreCase(code)) {       
        return;
      }
    }
    //  not belonging to this, check if code is already assigned to other labels
    Label lo = parent.isLabelCodeAssigned(this, code);
    if (lo != null) {
      throw new IllegalArgumentException(code + " is already assigned to " + lo);
    }
    // ok, insert   
    Labelcode lc = new LabelcodeHibernateImpl(code);
    List l = codes; // old trick, we know lc is a repo object
    InsertionSort.insert((List<RepositoryObjectHibernateImpl>)l, (RepositoryObjectHibernateImpl) lc);
    // save this to get ref in join table !
    parent.save(this);
   
  }
 
  /**
   * Gets the group of parts of the passed <code>String</code>
   * and matches them against the parts of this <code>String</code>.
   * @param s a non-<code>null</code> array of parts
   */
  public String matchParts(final String[] pts) {
   
    if (parts == null) { // only one part for 'this'
      String n = getName();
      for (String p : pts) {
        if (n.equalsIgnoreCase(p)) {
          return n;
        }
      }     
      return null; // not found
    } else {     
      // tedious o�: match every part in this against the other parts
      for (String mp : parts) {
        for (String op : pts) {
          if (mp.equalsIgnoreCase(op)) {
            return mp;
          }
        }
      }
      return null; // not found
    }   
  } 
 
  // XXX need factory get like in Repository, remove #add !!!
  public Labelcode get(int index) {
    return codes.get(index);
  }
 
  /* (non-Javadoc)
   * @see dnb.data.Label#size()
   */
  public int size() {
    return codes.size();
  }
 
 
  @Override
  public String toString() {   
    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  }

  RepositoryHibernateImpl getParent() {
    return parent;
  }
 

  List<LabelcodeHibernateImpl> getCodes() {
    return codes;
  }

  public String getNormalizedName() {
    return normalizedName;
  }
}
TOP

Related Classes of dnb.data.impl.LabelHibernateImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.