| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| POSTServiceRequestData |
|
| 1.6666666666666667;1.667 |
| 1 | package org.codeforamerica.open311.facade.data.operations; | |
| 2 | ||
| 3 | import java.util.HashMap; | |
| 4 | import java.util.LinkedList; | |
| 5 | import java.util.List; | |
| 6 | import java.util.Map; | |
| 7 | import java.util.regex.Matcher; | |
| 8 | import java.util.regex.Pattern; | |
| 9 | ||
| 10 | import org.codeforamerica.open311.facade.data.Attribute; | |
| 11 | import org.codeforamerica.open311.internals.parsing.DataParser; | |
| 12 | ||
| 13 | /** | |
| 14 | * Contains all the necessary data to add a new service request to the server. | |
| 15 | * It allows to add some optional arguments through the method chaining | |
| 16 | * (setters). | |
| 17 | * | |
| 18 | * @author Santiago MunĂn <santimunin@gmail.com> | |
| 19 | * | |
| 20 | */ | |
| 21 | public class POSTServiceRequestData { | |
| 22 | /** | |
| 23 | * Required email syntax. | |
| 24 | */ | |
| 25 | 1 | private static final Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile( |
| 26 | "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", | |
| 27 | Pattern.CASE_INSENSITIVE); | |
| 28 | private Map<String, String> parameters; | |
| 29 | private List<Attribute> attributes; | |
| 30 | ||
| 31 | /** | |
| 32 | * Basic constructor. It isn't public because this operation needs at least | |
| 33 | * one of the location parameters in addition to the service code. | |
| 34 | * | |
| 35 | * @param serviceCode | |
| 36 | * Code of the service related to the request. | |
| 37 | * @param attributes | |
| 38 | * List of attributes, <code>null</code> value is allowed. | |
| 39 | */ | |
| 40 | private POSTServiceRequestData(String serviceCode, | |
| 41 | 5 | List<Attribute> attributes) { |
| 42 | 5 | this.parameters = new HashMap<String, String>(); |
| 43 | 5 | this.attributes = new LinkedList<Attribute>(); |
| 44 | 5 | if (serviceCode != null && serviceCode.length() > 0) { |
| 45 | 5 | parameters.put(DataParser.SERVICE_CODE_TAG, serviceCode); |
| 46 | 5 | if (attributes != null) { |
| 47 | 0 | this.attributes = attributes; |
| 48 | } | |
| 49 | } else { | |
| 50 | 0 | throw new NoSuchFieldError( |
| 51 | "One or more of the arguments are not valid."); | |
| 52 | } | |
| 53 | 5 | } |
| 54 | ||
| 55 | /** | |
| 56 | * Builds a POST service request data object from a lat & long location | |
| 57 | * value. | |
| 58 | * | |
| 59 | * @param serviceCode | |
| 60 | * Code of the service related to the request. | |
| 61 | * @param latitude | |
| 62 | * Latitude using the (WGS84) projection. | |
| 63 | * @param longitude | |
| 64 | * Longitude using the (WGS84) projection. | |
| 65 | * @param attributes | |
| 66 | * List of attributes. | |
| 67 | */ | |
| 68 | public POSTServiceRequestData(String serviceCode, float latitude, | |
| 69 | float longitude, List<Attribute> attributes) { | |
| 70 | 1 | this(serviceCode, attributes); |
| 71 | 1 | this.setLatLong(latitude, longitude); |
| 72 | 1 | } |
| 73 | ||
| 74 | /** | |
| 75 | * Builds a POST service request data object from an address location value. | |
| 76 | * | |
| 77 | * @param serviceCode | |
| 78 | * Code of the service related to the request. | |
| 79 | * @param address | |
| 80 | * Human-readable address. | |
| 81 | * @param attributes | |
| 82 | * List of attributes. | |
| 83 | */ | |
| 84 | public POSTServiceRequestData(String serviceCode, String address, | |
| 85 | List<Attribute> attributes) { | |
| 86 | 1 | this(serviceCode, attributes); |
| 87 | 1 | if (address != null && address.length() > 0) { |
| 88 | 1 | this.setAddress(address); |
| 89 | } else { | |
| 90 | 0 | throw new NoSuchFieldError( |
| 91 | "One or more of the arguments are not valid."); | |
| 92 | } | |
| 93 | 1 | } |
| 94 | ||
| 95 | /** | |
| 96 | * Builds a POST service request data object from an address_id location | |
| 97 | * value. | |
| 98 | * | |
| 99 | * @param serviceCode | |
| 100 | * Code of the service related to the request. | |
| 101 | * @param addressId | |
| 102 | * The internal address ID used by a jurisdiction's master | |
| 103 | * address repository or other addressing system. | |
| 104 | * @param attributes | |
| 105 | * List of attributes. | |
| 106 | */ | |
| 107 | public POSTServiceRequestData(String serviceCode, long addressId, | |
| 108 | List<Attribute> attributes) { | |
| 109 | 3 | this(serviceCode, attributes); |
| 110 | 3 | this.setAddressId(addressId); |
| 111 | 3 | } |
| 112 | ||
| 113 | /** | |
| 114 | * Sets a (latitude, longitude) coordinates. | |
| 115 | * | |
| 116 | * @param latitude | |
| 117 | * Latitude using the (WGS84) projection. | |
| 118 | * @param longitude | |
| 119 | * Longitude using the (WGS84) projection. | |
| 120 | * @return Same instance with the new parameter added. | |
| 121 | */ | |
| 122 | public POSTServiceRequestData setLatLong(float latitude, float longitude) { | |
| 123 | 1 | parameters.put(DataParser.LATITUDE_TAG, String.valueOf(latitude)); |
| 124 | 1 | parameters.put(DataParser.LONGITUDE_TAG, String.valueOf(longitude)); |
| 125 | 1 | return this; |
| 126 | } | |
| 127 | ||
| 128 | /** | |
| 129 | * Sets an address parameter. | |
| 130 | * | |
| 131 | * @param address | |
| 132 | * Human-readable address. | |
| 133 | * @return Same instance with the new parameter added. | |
| 134 | */ | |
| 135 | public POSTServiceRequestData setAddress(String address) { | |
| 136 | 2 | tryToAddString(DataParser.ADDRESS_TAG, address); |
| 137 | 2 | return this; |
| 138 | } | |
| 139 | ||
| 140 | /** | |
| 141 | * Sets an address_id parameter. | |
| 142 | * | |
| 143 | * @param addressId | |
| 144 | * The internal address ID used by a jurisdiction's master | |
| 145 | * address repository or other addressing system. | |
| 146 | * @return Same instance with the new parameter added. | |
| 147 | */ | |
| 148 | public POSTServiceRequestData setAddressId(long addressId) { | |
| 149 | 3 | tryToAddString(DataParser.ADDRESS_ID_TAG, String.valueOf(addressId)); |
| 150 | 3 | return this; |
| 151 | } | |
| 152 | ||
| 153 | /** | |
| 154 | * Sets an email. | |
| 155 | * | |
| 156 | * @param email | |
| 157 | * The email address of the person submitting the request. It | |
| 158 | * requires a correct email syntax. | |
| 159 | * @return Same instance with the new parameter added. | |
| 160 | */ | |
| 161 | public POSTServiceRequestData setEmail(String email) { | |
| 162 | 0 | Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(email); |
| 163 | 0 | if (matcher.find()) { |
| 164 | 0 | tryToAddString(DataParser.EMAIL_TAG, email); |
| 165 | } | |
| 166 | 0 | return this; |
| 167 | } | |
| 168 | ||
| 169 | /** | |
| 170 | * Sets a device_id. | |
| 171 | * | |
| 172 | * @param deviceId | |
| 173 | * The unique device ID of the device submitting the request. | |
| 174 | * This is usually only used for mobile devices. Android devices | |
| 175 | * can use <code>TelephonyManager.getDeviceId()</code> and | |
| 176 | * iPhones can use | |
| 177 | * <code>[UIDevice currentDevice].uniqueIdentifier</code>. | |
| 178 | * | |
| 179 | * @return Same instance with the new parameter added. | |
| 180 | */ | |
| 181 | public POSTServiceRequestData setDeviceId(String deviceId) { | |
| 182 | 2 | tryToAddString(DataParser.DEVICE_ID_TAG, deviceId); |
| 183 | 2 | return this; |
| 184 | } | |
| 185 | ||
| 186 | /** | |
| 187 | * Sets an account_id. | |
| 188 | * | |
| 189 | * @param accountId | |
| 190 | * The unique ID for the user account of the person submitting | |
| 191 | * the request. | |
| 192 | * @return Same instance with the new parameter added. | |
| 193 | */ | |
| 194 | public POSTServiceRequestData setAccountId(long accountId) { | |
| 195 | 0 | tryToAddString(DataParser.ACCOUNT_ID_TAG, String.valueOf(accountId)); |
| 196 | 0 | return this; |
| 197 | } | |
| 198 | ||
| 199 | /** | |
| 200 | * Sets the first_name. | |
| 201 | * | |
| 202 | * @param firstName | |
| 203 | * The given name of the person submitting the request. | |
| 204 | * @return Same instance with the new parameter added. | |
| 205 | */ | |
| 206 | public POSTServiceRequestData setFirstName(String firstName) { | |
| 207 | 0 | tryToAddString(DataParser.FIRST_NAME_TAG, firstName); |
| 208 | 0 | return this; |
| 209 | } | |
| 210 | ||
| 211 | /** | |
| 212 | * Sets the last_name. | |
| 213 | * | |
| 214 | * @param lastName | |
| 215 | * The family name of the person submitting the request . | |
| 216 | * @return Same instance with the new parameter added. | |
| 217 | */ | |
| 218 | public POSTServiceRequestData setLastName(String lastName) { | |
| 219 | 0 | tryToAddString(DataParser.LAST_NAME_TAG, lastName); |
| 220 | 0 | return this; |
| 221 | } | |
| 222 | ||
| 223 | /** | |
| 224 | * Sets the phone. | |
| 225 | * | |
| 226 | * @param phone | |
| 227 | * The phone number of the person submitting the request. | |
| 228 | * @return Same instance with the new parameter added. | |
| 229 | */ | |
| 230 | public POSTServiceRequestData setPhone(String phone) { | |
| 231 | 0 | tryToAddString(DataParser.PHONE_TAG, phone); |
| 232 | 0 | return this; |
| 233 | } | |
| 234 | ||
| 235 | /** | |
| 236 | * Sets a description. | |
| 237 | * | |
| 238 | * @param description | |
| 239 | * A full description of the request or report being submitted. | |
| 240 | * This may contain line breaks, but not HTML or code. Otherwise, | |
| 241 | * this is free form text limited to 4,000 characters. | |
| 242 | * @return Same instance with the new parameter added. | |
| 243 | */ | |
| 244 | public POSTServiceRequestData setDescription(String description) { | |
| 245 | 1 | tryToAddString(DataParser.DESCRIPTION_TAG, description); |
| 246 | 1 | return this; |
| 247 | } | |
| 248 | ||
| 249 | /** | |
| 250 | * Sets a media URL. | |
| 251 | * | |
| 252 | * @param mediaUrl | |
| 253 | * A URL to media associated with the request, eg an image. | |
| 254 | * @return Same instance with the new parameter added. | |
| 255 | */ | |
| 256 | public POSTServiceRequestData setMediaUrl(String mediaUrl) { | |
| 257 | 1 | tryToAddString(DataParser.MEDIA_URL_TAG, mediaUrl); |
| 258 | 1 | return this; |
| 259 | } | |
| 260 | ||
| 261 | /** | |
| 262 | * Tries to add a pair (key, value) to the parameter list. Key and value has | |
| 263 | * to be valid parameters (not null and not empty). | |
| 264 | * | |
| 265 | * @param key | |
| 266 | * Key of the pair. | |
| 267 | * @param value | |
| 268 | * Value of the pair. | |
| 269 | */ | |
| 270 | private void tryToAddString(String key, String value) { | |
| 271 | 9 | if (key != null && key.length() > 0 && value != null |
| 272 | && value.length() > 0) { | |
| 273 | 5 | parameters.put(key, value); |
| 274 | } | |
| 275 | 9 | } |
| 276 | ||
| 277 | /** | |
| 278 | * Builds a map containing all the arguments. | |
| 279 | * | |
| 280 | * @return List of pairs (key, value) with the required arguments. | |
| 281 | */ | |
| 282 | public Map<String, String> getBodyRequestParameters() { | |
| 283 | 20 | Map<String, String> result = new HashMap<String, String>(); |
| 284 | 20 | result.putAll(parameters); |
| 285 | 20 | return result; |
| 286 | } | |
| 287 | ||
| 288 | /** | |
| 289 | * Obtains the attributes related to the object. | |
| 290 | * | |
| 291 | * @return A copy of the inner attribute list. | |
| 292 | */ | |
| 293 | public List<Attribute> getAttributes() { | |
| 294 | 10 | List<Attribute> result = new LinkedList<Attribute>(); |
| 295 | 10 | result.addAll(attributes); |
| 296 | 10 | return result; |
| 297 | } | |
| 298 | ||
| 299 | } |