View Javadoc
1 /* 2 * ==================================================================== 3 * 4 * This code is subject to the freebxml License, Version 1.1 5 * 6 * Copyright (c) 2003 freebxml.org. All rights reserved. 7 * 8 * ==================================================================== 9 */ 10 package com.sun.xml.registry.client.browser.registration; 11 12 import java.util.Collection; 13 import java.util.HashMap; 14 import java.util.Iterator; 15 import java.util.Map; 16 17 import javax.xml.registry.JAXRException; 18 import javax.xml.registry.infomodel.PostalAddress; 19 import javax.xml.registry.infomodel.User; 20 21 import com.sun.xml.registry.client.browser.swing.RegistryMappedModel; 22 23 /*** 24 * @author Fabian Ritzmann 25 */ 26 public class PostalAddressModel extends RegistryMappedModel { 27 28 public static final String[] ADDRESS_TYPES = { 29 "Home", 30 "Office", 31 "Vacation", 32 "Temporary" 33 }; 34 35 private final User user; 36 private final Map addresses = new HashMap(); 37 38 39 PostalAddressModel(User u) { 40 super(ADDRESS_TYPES[0]); 41 this.user = u; 42 } 43 44 45 /*** 46 * Method setStreetNum. 47 * @param addressType 48 * @param text 49 */ 50 public void setStreetNum(String streetNum) 51 throws JAXRException { 52 53 streetNum = streetNum.trim(); 54 55 if (this.addresses.containsKey(this.key)) { 56 // Address already exists, just need to modify entry. 57 PostalAddress postalAddress = 58 (PostalAddress) this.addresses.get(this.key); 59 postalAddress.setStreetNumber(streetNum); 60 } 61 else { 62 // Create new address and add to user. 63 PostalAddress postalAddress = this.user.getLifeCycleManager().createPostalAddress(streetNum, "", "", "", "", "", this.key); 64 this.addresses.put(this.key, postalAddress); 65 this.user.setPostalAddresses(this.addresses.values()); 66 } 67 } 68 69 /*** 70 * Method setStreet. 71 * @param addressType 72 * @param text 73 */ 74 public void setStreet(String street) 75 throws JAXRException { 76 77 street = street.trim(); 78 79 if (this.addresses.containsKey(this.key)) { 80 // Address already exists, just need to modify entry. 81 PostalAddress postalAddress = (PostalAddress) this.addresses.get(this.key); 82 postalAddress.setStreet(street); 83 } 84 else { 85 // Create new address and add to user. 86 PostalAddress postalAddress = this.user.getLifeCycleManager().createPostalAddress("", street, "", "", "", "", this.key); 87 this.addresses.put(this.key, postalAddress); 88 this.user.setPostalAddresses(this.addresses.values()); 89 } 90 } 91 92 /*** 93 * Method setCity. 94 * @param addressType 95 * @param text 96 */ 97 public void setCity(String city) 98 throws JAXRException { 99 100 city = city.trim(); 101 102 if (this.addresses.containsKey(this.key)) { 103 // Address already exists, just need to modify entry. 104 PostalAddress postalAddress = (PostalAddress) this.addresses.get(this.key); 105 postalAddress.setCity(city); 106 } 107 else { 108 // Create new address and add to user. 109 PostalAddress postalAddress = this.user.getLifeCycleManager().createPostalAddress("", "", city, "", "", "", this.key); 110 this.addresses.put(this.key, postalAddress); 111 this.user.setPostalAddresses(this.addresses.values()); 112 } 113 } 114 115 /*** 116 * Method setState. 117 * @param addressType 118 * @param text 119 */ 120 public void setState(String state) 121 throws JAXRException { 122 123 state = state.trim(); 124 125 if (this.addresses.containsKey(this.key)) { 126 // Address already exists, just need to modify entry. 127 PostalAddress postalAddress = (PostalAddress) this.addresses.get(this.key); 128 postalAddress.setStateOrProvince(state); 129 } 130 else { 131 // Create new address and add to user. 132 PostalAddress postalAddress = this.user.getLifeCycleManager().createPostalAddress("", "", "", state, "", "", this.key); 133 this.addresses.put(this.key, postalAddress); 134 this.user.setPostalAddresses(this.addresses.values()); 135 } 136 } 137 138 /*** 139 * Method setPostalCode. 140 * @param addressType 141 * @param text 142 */ 143 public void setPostalCode(String code) 144 throws JAXRException { 145 146 code = code.trim(); 147 148 if (this.addresses.containsKey(this.key)) { 149 // Address already exists, just need to modify entry. 150 PostalAddress postalAddress = (PostalAddress) this.addresses.get(this.key); 151 postalAddress.setPostalCode(code); 152 } 153 else { 154 // Create new address and add to user. 155 PostalAddress postalAddress = this.user.getLifeCycleManager().createPostalAddress("", "", "", "", "", code, this.key); 156 this.addresses.put(this.key, postalAddress); 157 this.user.setPostalAddresses(this.addresses.values()); 158 } 159 } 160 161 /*** 162 * Method setCountry. 163 * @param addressType 164 * @param text 165 */ 166 public void setCountry(String country) 167 throws JAXRException { 168 169 country = country.trim(); 170 171 if (this.addresses.containsKey(this.key)) { 172 // Address already exists, just need to modify entry. 173 PostalAddress postalAddress = (PostalAddress) this.addresses.get(this.key); 174 postalAddress.setCountry(country); 175 } 176 else { 177 // Create new address and add to user. 178 PostalAddress postalAddress = this.user.getLifeCycleManager().createPostalAddress("", "", "", "", country, "", this.key); 179 this.addresses.put(this.key, postalAddress); 180 this.user.setPostalAddresses(this.addresses.values()); 181 } 182 } 183 184 185 public PostalAddress getAddress() { 186 PostalAddress address = (PostalAddress) this.addresses.get(this.key); 187 return address; 188 } 189 190 191 public void validate() throws JAXRException { 192 193 // Remove empty addresses 194 Collection addressSet = this.addresses.values(); 195 PostalAddress address = null; 196 Iterator i = addressSet.iterator(); 197 while (i.hasNext()) { 198 address = (PostalAddress) i.next(); 199 String city = address.getCity(); 200 String country = address.getCountry(); 201 String code = address.getPostalCode(); 202 String state = address.getStateOrProvince(); 203 String street = address.getStreet(); 204 String number = address.getStreetNumber(); 205 // Takes all fields except the address type into account. 206 if (((city == null) || (city.length() == 0)) 207 && ((country == null) || (country.length() == 0)) 208 && ((code == null) || (code.length() == 0)) 209 && ((state == null) || (state.length() == 0)) 210 && ((street == null) || (street.length() == 0)) 211 && ((number == null) || (number.length() == 0))) { 212 213 this.addresses.remove(address.getType()); 214 } 215 } 216 this.user.setPostalAddresses(this.addresses.values()); 217 218 // Check if at least one address is set. 219 if (this.addresses.isEmpty()) { 220 throw new JAXRException("Missing required PostalAddress on User"); 221 } 222 } 223 224 }

This page was automatically generated by Maven