001 /*----------------------------------------------------------------------- 002 * Copyright (C) 2006 003 * Associated Universities, Inc. Washington DC, USA. 004 * This program is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU General Public License as 006 * published by the Free Software Foundation; either version 2 of 007 * the License, or (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * Correspondence concerning this software should be addressed as follows: 015 * Internet email: switz@nrao.edu 016 * Postal address: User Database 017 * National Radio Astronomy Observatory 018 * Post Office Box 0 019 * Socorro, NM 87801 USA 020 *-----------------------------------------------------------------------*/ 021 package edu.nrao.sss.model.user; 022 023 import java.util.Collection; 024 import java.util.Date; 025 import java.util.HashSet; 026 import java.util.Iterator; 027 import java.util.Set; 028 029 import edu.nrao.sss.xml.XMLTransform; 030 031 public class Institution extends ContactCollection implements XMLable { 032 033 private static final long serialVersionUID = 1L; 034 035 private Set<User> affiliatedUsers; 036 037 private String contactPerson1; 038 039 private String contactPerson2; 040 041 private String departmentName; 042 043 private EntryStatus entryStatus; 044 045 private String formalName; 046 047 private String mailingName; 048 049 private int parentId; 050 051 private String shortName; 052 053 private Set<InstitutionType> types; 054 055 public void addToAffiliatedUsers(User user) { 056 if (user == null) 057 return; 058 if (affiliatedUsers == null) 059 affiliatedUsers = new HashSet<User>(); 060 affiliatedUsers.add(user); 061 } 062 063 public void addToTypes(InstitutionType institutionType) { 064 if (institutionType == null) 065 return; 066 if (types == null) 067 types = new HashSet<InstitutionType>(); 068 types.add(institutionType); 069 } 070 071 public boolean equals(Object obj) { 072 if (null == obj) 073 return false; 074 if (!(obj instanceof Institution)) 075 return false; 076 else { 077 Institution mObj = (Institution) obj; 078 return (this.getId() == mObj.getId()); 079 } 080 } 081 082 public Set<User> getAffiliatedUsers() { 083 if (affiliatedUsers == null) 084 affiliatedUsers = new HashSet<User>(); 085 return affiliatedUsers; 086 } 087 088 public String getContactPerson1() { 089 return contactPerson1; 090 } 091 092 public String getContactPerson2() { 093 return contactPerson2; 094 } 095 096 public String getDepartmentName() { 097 return departmentName; 098 } 099 100 public EntryStatus getEntryStatus() { 101 return entryStatus; 102 } 103 104 public String getFormalName() { 105 return formalName; 106 } 107 108 public String getMailingName() { 109 return mailingName; 110 } 111 112 public String getNameAndAcronym() { 113 if (formalName == null) 114 return "Unknown Institution"; 115 else if (shortName == null || shortName.trim().length() < 1) 116 return formalName; 117 else 118 return formalName + " (" + shortName + ")"; 119 } 120 121 public int getParentId() { 122 return parentId; 123 } 124 125 public String getShortName() { 126 return shortName; 127 } 128 129 public Set<InstitutionType> getTypes() { 130 if (types == null) 131 types = new HashSet<InstitutionType>(); 132 return types; 133 } 134 135 private String institutionBodyXML(boolean showAll) { 136 StringBuffer result = new StringBuffer(); 137 138 if (getFormalName() != null) 139 result 140 .append("<formal-name>" 141 + XMLTransform.encode(getFormalName()) 142 + "</formal-name>\n"); 143 if (showAll && getMailingName() != null) 144 result.append("<mailing-name>" 145 + XMLTransform.encode(getMailingName()) 146 + "</mailing-name>\n"); 147 if (showAll && getDepartmentName() != null) 148 result.append("<department-name>" 149 + XMLTransform.encode(getDepartmentName()) 150 + "</department-name>\n"); 151 if (showAll && getShortName() != null) 152 result.append("<short-name>" + XMLTransform.encode(getShortName()) 153 + "</short-name>\n"); 154 if (showAll && getContactPerson1() != null) 155 result.append("<contact-person>" 156 + XMLTransform.encode(getContactPerson1()) 157 + "</contact-person>\n"); 158 if (showAll && getContactPerson2() != null) 159 result.append("<contact-person>" 160 + XMLTransform.encode(getContactPerson2()) 161 + "</contact-person>\n"); 162 163 return result.toString(); 164 } 165 166 public boolean isDomestic() { 167 for (Iterator<InstitutionType> iter = getTypes().iterator(); iter 168 .hasNext();) { 169 if (iter.next().getInstitutionType().equalsIgnoreCase( 170 InstitutionType.DOMESTIC_NAME)) 171 return true; 172 } 173 return false; 174 } 175 176 public void setAffiliatedUsers(Set<User> affiliatedUsers) { 177 this.affiliatedUsers = affiliatedUsers; 178 } 179 180 public void setContactPerson1(String contactPerson1) { 181 this.contactPerson1 = contactPerson1; 182 } 183 184 public void setContactPerson2(String contactPerson2) { 185 this.contactPerson2 = contactPerson2; 186 } 187 188 public void setDepartmentName(String departmentName) { 189 this.departmentName = departmentName; 190 } 191 192 public void setEntryStatus(EntryStatus entryStatus) { 193 this.entryStatus = entryStatus; 194 } 195 196 public void setFormalName(String formalName) { 197 this.formalName = formalName; 198 } 199 200 public void setMailingName(String mailingName) { 201 this.mailingName = mailingName; 202 } 203 204 public void setParentId(int parentId) { 205 this.parentId = parentId; 206 } 207 208 public void setShortName(String shortName) { 209 this.shortName = shortName; 210 } 211 212 public void setTypes(Set<InstitutionType> types) { 213 this.types = types; 214 } 215 216 public String toShortXML(boolean isDefault) { 217 StringBuffer sb = new StringBuffer(); 218 String fName, dName; 219 sb.append("<institution-affiliation>\n"); 220 sb.append("<institution-id>" + getId() + "</institution-id>\n"); 221 sb.append("<default>" + isDefault + "</default>\n"); 222 fName = getFormalName(); 223 if (fName != null && fName.length() > 0) 224 sb.append("<formal-name>" + XMLTransform.encode(fName) 225 + "</formal-name>\n"); 226 dName = getDepartmentName(); 227 if (dName != null && dName.trim().length() > 0) 228 sb.append("<department-name>" + XMLTransform.encode(dName) 229 + "</department-name>\n"); 230 if (isDomestic()) 231 sb.append("<domestic>true</domestic>\n"); 232 else 233 sb.append("<domestic>false</domestic>\n"); 234 sb.append("<country>" + getCountry() + "</country>\n"); 235 sb.append("</institution-affiliation>"); 236 return sb.toString(); 237 } 238 239 public final String toXML() { 240 StringBuffer sb = new StringBuffer(); 241 String fName, dName, mName, c1, c2; 242 Date upOn; 243 Set<InstitutionType> iTypes = getTypes(); 244 Collection<Contact> iContacts = getContactCollection(); 245 EntryStatus eS = getEntryStatus(); 246 247 sb.append("<institution>\n"); 248 sb.append("<id>" + getId() + "</id>\n"); 249 fName = getFormalName(); 250 if (fName != null && fName.trim().length() > 0) 251 sb.append("<formal-name>" + XMLTransform.encode(fName) 252 + "</formal-name>\n"); 253 254 mName = getMailingName(); 255 if (mName != null && mName.trim().length() > 0) 256 sb.append("<mailing-name>" + XMLTransform.encode(mName) 257 + "</mailing-name>\n"); 258 259 dName = getDepartmentName(); 260 if (dName != null && dName.trim().length() > 0) 261 sb.append("<department-name>" + XMLTransform.encode(dName) 262 + "</department-name>\n"); 263 264 c1 = getContactPerson1(); 265 if (c1 != null && c1.trim().length() > 0) 266 sb.append("<contact-person>" + XMLTransform.encode(c1) 267 + "</contact-person>\n"); 268 269 c2 = getContactPerson2(); 270 if (c2 != null && c2.trim().length() > 0) 271 sb.append("<contact-person>" + XMLTransform.encode(c2) 272 + "</contact-person>\n"); 273 274 if (iTypes != null) 275 for (Iterator<InstitutionType> iter = iTypes.iterator(); iter.hasNext();) { 276 InstitutionType t = iter.next(); 277 sb.append(t.toXML()); 278 } 279 280 if (iContacts != null) 281 for (Iterator<Contact> iter = iContacts.iterator(); iter.hasNext();) { 282 Contact c = iter.next(); 283 sb.append(c.toXML()); 284 } 285 286 if (eS != null) { 287 sb.append(eS.toXML()); 288 } 289 290 upOn = getUpdatedOn(); 291 if (upOn != null) 292 sb.append("<updated-on>" 293 + XMLTransform.encode(getUpdatedOn().toString()) 294 + "</updated-on>\n"); 295 296 sb.append("<updated-by>" + getUpdatedBy() + "</updated-by>\n"); 297 298 sb.append("</institution>\n"); 299 return sb.toString(); 300 301 } 302 303 public String toXML(boolean showAll, boolean forUser, boolean isDefault) { 304 StringBuffer result = new StringBuffer(); 305 if (forUser) { 306 if (isDefault) 307 result.append("<default-affiliation"); 308 else 309 result.append("<additional-affiliation"); 310 } else { 311 result.append("<institution"); 312 } 313 if (!isNew()) 314 result.append(" id=\"" + getId() + "\">\n"); 315 else 316 result.append(">\n"); 317 result.append(institutionBodyXML(showAll)); 318 if (forUser) { 319 if (isDefault) 320 result.append("</default-affiliation>\n"); 321 else 322 result.append("</additional-affiliation>\n"); 323 } else { 324 result.append("</institution>\n"); 325 } 326 return result.toString(); 327 } 328 329 }