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.TelephoneNumber; 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 TelephoneNumberModel extends RegistryMappedModel { 27 28 public static final String[] PHONE_TYPES = { 29 "Office Phone", 30 "Home Phone", 31 "Mobile Phone", 32 "Beeper", 33 "FAX" 34 }; 35 36 private final User user; 37 private final Map numbers = new HashMap(); 38 39 40 TelephoneNumberModel(User u) { 41 super(PHONE_TYPES[0]); 42 this.user = u; 43 } 44 45 46 /*** 47 * Method setCountryCode. 48 * @param phoneType 49 * @param text 50 */ 51 public void setCountryCode(String countryCode) 52 throws JAXRException { 53 54 countryCode = countryCode.trim(); 55 56 if (this.numbers.containsKey(this.key)) { 57 // Number already exists, just need to modify entry. 58 TelephoneNumber phoneNumber = 59 (TelephoneNumber) this.numbers.get(this.key); 60 phoneNumber.setCountryCode(countryCode); 61 } 62 else { 63 // Create new number and add to user. 64 TelephoneNumber phoneNumber = this.user.getLifeCycleManager().createTelephoneNumber(); 65 phoneNumber.setCountryCode(countryCode); 66 this.numbers.put(this.key, phoneNumber); 67 this.user.setTelephoneNumbers(this.numbers.values()); 68 } 69 } 70 71 72 /*** 73 * Method setAreaCode. 74 * @param phoneType 75 * @param text 76 */ 77 public void setAreaCode(String areaCode) 78 throws JAXRException { 79 80 areaCode = areaCode.trim(); 81 82 if (this.numbers.containsKey(this.key)) { 83 // Number already exists, just need to modify entry. 84 TelephoneNumber phoneNumber = (TelephoneNumber) this.numbers.get(this.key); 85 phoneNumber.setAreaCode(areaCode); 86 } 87 else { 88 // Create new number and add to user. 89 TelephoneNumber phoneNumber = this.user.getLifeCycleManager().createTelephoneNumber(); 90 phoneNumber.setAreaCode(areaCode); 91 this.numbers.put(this.key, phoneNumber); 92 this.user.setTelephoneNumbers(this.numbers.values()); 93 } 94 } 95 96 97 /*** 98 * Method setNumber. 99 * @param phoneType 100 * @param text 101 */ 102 public void setNumber(String number) 103 throws JAXRException { 104 105 number = number.trim(); 106 107 if (this.numbers.containsKey(this.key)) { 108 // Number already exists, just need to modify entry. 109 TelephoneNumber phoneNumber = (TelephoneNumber) this.numbers.get(this.key); 110 phoneNumber.setNumber(number); 111 } 112 else { 113 // Create new number and add to user. 114 TelephoneNumber phoneNumber = this.user.getLifeCycleManager().createTelephoneNumber(); 115 phoneNumber.setNumber(number); 116 this.numbers.put(this.key, phoneNumber); 117 this.user.setTelephoneNumbers(this.numbers.values()); 118 } 119 } 120 121 122 /*** 123 * Method setExtension. 124 * @param phoneType 125 * @param text 126 */ 127 public void setExtension(String extension) 128 throws JAXRException { 129 130 extension = extension.trim(); 131 132 if (this.numbers.containsKey(this.key)) { 133 // Number already exists, just need to modify entry. 134 TelephoneNumber phoneNumber = (TelephoneNumber) this.numbers.get(this.key); 135 phoneNumber.setExtension(extension); 136 } 137 else { 138 // Create new number and add to user. 139 TelephoneNumber phoneNumber = this.user.getLifeCycleManager().createTelephoneNumber(); 140 phoneNumber.setExtension(extension); 141 this.numbers.put(this.key, phoneNumber); 142 this.user.setTelephoneNumbers(this.numbers.values()); 143 } 144 } 145 146 147 /*** 148 * Method setURL. 149 * @param phoneType 150 * @param text 151 */ 152 public void setURL(String url) 153 throws JAXRException { 154 155 url = url.trim(); 156 157 if (this.numbers.containsKey(this.key)) { 158 // Number already exists, just need to modify entry. 159 TelephoneNumber phoneNumber = (TelephoneNumber) this.numbers.get(this.key); 160 phoneNumber.setUrl(url); 161 } 162 else { 163 // Create new number and add to user. 164 TelephoneNumber phoneNumber = this.user.getLifeCycleManager().createTelephoneNumber(); 165 phoneNumber.setUrl(url); 166 this.numbers.put(this.key, phoneNumber); 167 this.user.setTelephoneNumbers(this.numbers.values()); 168 } 169 } 170 171 172 public TelephoneNumber getNumber() { 173 TelephoneNumber number = (TelephoneNumber) this.numbers.get(this.key); 174 return number; 175 } 176 177 178 public void validate() throws JAXRException { 179 180 // Remove empty addresses 181 Collection numberSet = this.numbers.values(); 182 TelephoneNumber number = null; 183 Iterator i = numberSet.iterator(); 184 while (i.hasNext()) { 185 number = (TelephoneNumber) i.next(); 186 String areaCode = number.getAreaCode(); 187 String countryCode = number.getCountryCode(); 188 String extension = number.getExtension(); 189 String phoneNumber = number.getNumber(); 190 String url = number.getUrl(); 191 // Takes all fields except the address type into account. 192 if (((areaCode == null) || (areaCode.length() == 0)) 193 && ((countryCode == null) || (countryCode.length() == 0)) 194 && ((extension == null) || (extension.length() == 0)) 195 && ((phoneNumber == null) || (phoneNumber.length() == 0)) 196 && ((url == null) || (url.length() == 0))) { 197 198 this.numbers.remove(number.getType()); 199 } 200 } 201 this.user.setTelephoneNumbers(this.numbers.values()); 202 203 // Check if at least one number is set. 204 if (this.numbers.isEmpty()) { 205 throw new JAXRException("Missing required TelephoneNumber on User"); 206 } 207 } 208 209 }

This page was automatically generated by Maven