package de.hsadmin.mods.pac; import static javax.persistence.CascadeType.ALL; import static javax.persistence.FetchType.EAGER; import static javax.persistence.FetchType.LAZY; import static javax.persistence.GenerationType.SEQUENCE; import java.io.Serializable; import java.util.Date; import javax.persistence.EntityManager; import de.hsadmin.mods.cust.Customer; import de.hsadmin.mods.user.UnixUser; @javax.persistence.Entity(name = "Pacs") @javax.persistence.Table(name = "packet") @javax.persistence.SequenceGenerator(name = "PacsSeqGen", sequenceName = "packet_packet_id_seq") @de.hsadmin.core.model.EntityInfo(name = "Paket") public class Pac extends de.hsadmin.core.model.Entity implements Serializable { private static final long serialVersionUID = 1201899873300190132L; @javax.persistence.Id @javax.persistence.GeneratedValue(strategy = SEQUENCE, generator = "PacsSeqGen") @javax.persistence.Column(name = "packet_id") private long id; @javax.persistence.Column(name = "packet_name", unique = true) private String name; @javax.persistence.JoinColumn(name = "bp_id") @javax.persistence.ManyToOne(fetch = EAGER) private Customer customer; @javax.persistence.JoinColumn(name = "hive_id") @javax.persistence.ManyToOne(fetch = EAGER) private Hive hive; @javax.persistence.Column(name = "created") @javax.persistence.Temporal(javax.persistence.TemporalType.DATE) private Date created; @javax.persistence.Column(name = "cancelled", nullable = true) @javax.persistence.Temporal(javax.persistence.TemporalType.DATE) private Date cancelled; @javax.persistence.Column(name = "order_number", nullable = true) private String orderNo; @javax.persistence.Column(name = "webserver_group", nullable = true) private String webserverGroup; @javax.persistence.JoinColumn(name = "cur_inet_addr_id", nullable = true) @javax.persistence.ManyToOne(fetch = EAGER) private INetAddress curINetAddr; @javax.persistence.JoinColumn(name = "old_inet_addr_id", nullable = true) @javax.persistence.ManyToOne(fetch = EAGER) private INetAddress oldINetAddr; @javax.persistence.OneToMany(fetch = LAZY, cascade = ALL) private java.util.Set pacComponents; @javax.persistence.OneToMany(fetch = LAZY, cascade = ALL, mappedBy = "pac") private java.util.Set unixUser; public Pac() { } public Pac(String name, Customer cust, BasePac basePac, Hive hive) { this.name = name; this.customer = cust; this.hive = hive; this.created = new java.util.Date(); this.webserverGroup = "httpd"; this.curINetAddr = hive.getInetAddr(); pacComponents = new java.util.HashSet(); java.util.Date today = new java.util.Date(); for (Component comp : basePac.getComponents()) { PacComponent pacComp = new PacComponent(basePac, comp.getBaseComponent(), this, comp.getDefaultQuantity(), today, null); pacComponents.add(pacComp); System.err.println("TRACE [Pac]: added pacComp " + pacComp + " to " + getName()); } } // / {@inheritDoc} public static String createQueryFromStringKey(String humanKey) { return "obj.name='" + humanKey + "'"; } // / {@inheritDoc} @Override public String createStringKey() { return getName(); } // / {@inheritDoc} @Override public long id() { return id; } public long getId() { return id; } protected void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public Hive getHive() { return hive; } public void setHive(Hive hive) { this.hive = hive; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public Date getCancelled() { return cancelled; } public void setCancelled(Date cancelled) { this.cancelled = cancelled; } public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public String getwebserverGroup() { return webserverGroup; } public void setwebserverGroup(String webserverGroup) { this.webserverGroup = webserverGroup; } public INetAddress getCurINetAddr() { return curINetAddr; } public void setCurINetAddr(INetAddress curINetAddr) { this.curINetAddr = curINetAddr; } public INetAddress getOldINetAddr() { return oldINetAddr; } public void setOldINetAddr(INetAddress oldINetAddr) { this.oldINetAddr = oldINetAddr; } // virtual attribute basePac /* * This does not work, JPA/Hibernate always wants to create an invalid row * in packet_component * * @javax.persistence.ManyToOne(fetch=EAGER, optional=true) // optional * should be default anyway * * @javax.persistence.JoinTable( name="packet_component", * joinColumns=@javax.persistence.JoinColumn(name="packet_id"), * inverseJoinColumns=@javax.persistence.JoinColumn(name="basepacket_id") ) */ @javax.persistence.Transient public BasePac getBasePac() { return getPacComponents().iterator().next().getBasePac(); } public void setBasePac(BasePac basePac) { // TODO: needs code to change basePac in all pacComponents } public java.util.Set getPacComponents() { return pacComponents; } public void setPacComponents(java.util.Set pacComponents) { this.pacComponents = pacComponents; } public PacComponent getPacComponent(String feature) { for (PacComponent pc : getPacComponents()) if (feature.equals(pc.getBaseComponent().getFeature())) return pc; return null; } public java.util.Set getUnixUser() { return unixUser; } public void setUnixUser(java.util.Set unixUser) { this.unixUser = unixUser; } // generic public String toString() { return super.toString() + "{ name=" + name + " }"; } // / {$inheritDoc} @Override public boolean isNew() { return id == 0; } // / {$inheritDoc} @Override public String getHiveName() { if (isNew()) return null; else return getHive().getHiveName(); } // / {@inheritDoc} @Override public UnixUser owningUser(EntityManager em) { return customer.owningUser(em); } public UnixUser getAdminUser(EntityManager em) { // TODO Auto-generated method stub return null; } // / determines whether the given user has full read access on all merged // fields of this entity public boolean isReadAllowedFor(UnixUser loginUser) { return getName().equals(loginUser.getName()) || super.isReadAllowedFor(loginUser); } // / query restriction for access control public static String restriction() { return // all pacs of customer "obj.customer.name=:loginUserName OR " + // pac of packet admin "obj.name=:loginUserName"; } }