View Javadoc

1   package gov.bnl.gums;
2   
3   import gov.bnl.gums.account.AccountMapper;
4   
5   import java.lang.ref.SoftReference;
6   
7   import javax.persistence.*;
8   
9   @Entity
10  public class Mapping {
11  	protected static String[] stateTypes = {"available", "assigned", "suspended"};
12  	protected int stateIndex = 0;
13  	
14  	// persistent fields
15  	protected long id;
16  	protected String dn;
17  	protected SiteUser siteUser = new SiteUser();
18  	protected SoftReference<AccountMapper> accountMapperRef = null;
19  	
20  	public Mapping() {}
21  
22  	public Mapping(String dn, SiteUser siteUser) {
23  		assert(siteUser != null);
24  		this.dn = dn;
25  		this.siteUser = siteUser;
26  	}
27  	
28  	public Mapping(String dn, SiteUser siteUser, String state) {
29  		assert(siteUser != null);
30  		this.dn = dn;
31  		this.siteUser = siteUser;
32  		setState(state);
33  	}
34  
35  	public boolean equals(Object o) {
36  		if (o == null)
37  			return false;
38  		else if (o instanceof Mapping) {
39  			Mapping mapping = (Mapping)o;
40  			assert(siteUser != null);
41  			if (siteUser==null)
42  				throw new RuntimeException("siteuser null");
43  			assert(((Mapping)o).getSiteUser() != null);
44  			if (dn==null && mapping.dn==null)
45  				return true;
46  			else if (dn==null || mapping.dn==null)
47  				return false;
48  			else 
49  				return dn.equals(mapping.getDn()) && siteUser.equals(mapping.getSiteUser());
50  		}
51  		else if (o instanceof SiteUser)
52  			return siteUser.equals((SiteUser)o);
53  		else if (o instanceof GridUser && dn!=null)
54  			return dn.equals((GridUser)o);
55  		else
56  			return false;
57  	}
58  	
59  	@ManyToOne
60      @JoinColumn(name="accountMapper")
61      public AccountMapper getAccountMapper() {
62  		return accountMapperRef == null ? null : accountMapperRef.get();
63  	}
64  
65  	public String getDn() {
66  		return dn;
67  	}
68  	
69      public SiteUser getSiteUser() {
70  		return siteUser;
71  	}
72      
73  	public String getState() {
74  		return stateTypes[stateIndex];
75  	}
76  
77  	public int hashCode() {
78      	return dn!=null ? 10*dn.hashCode() : 0 + 10*dn.hashCode() + siteUser.hashCode();
79      }
80  
81  	public void setAccountMapper(AccountMapper accountMapper) {
82  		this.accountMapperRef = new SoftReference<AccountMapper>(accountMapper);
83  	}
84  
85  	public void setDn(String dn) {
86  		this.dn = dn;
87  	}
88  
89  	public void setSiteUser(SiteUser siteUser) {
90  		assert(siteUser != null):
91  		this.siteUser = siteUser;
92  	}
93  
94  	public void setState(String state) {
95  		if (state==null)
96  			return;
97      	for(int i=0; i<stateTypes.length; i++) {
98      		if ( stateTypes[i].equalsIgnoreCase(state) ) {
99      			stateIndex = i;
100     			return;
101     		}
102     	}
103     	throw new RuntimeException("Invalid state type: "+state);
104 	}
105 
106 	public String toString() {
107     	return dn + "," + siteUser.toString();
108     }
109 
110 	@Id
111 	@GeneratedValue(strategy = GenerationType.AUTO)
112 	@SuppressWarnings("unused")
113 	private long getId() {
114 		return id;
115 	}
116 
117 	@SuppressWarnings("unused")
118 	private void setId(long id) {
119 		this.id = id;
120 	}
121 }