ORVE WS (Dynamic) (8) Organizing class structure
0. Introduction
We have got a bunch of classes. It's time to organize our code. One option is creating a class with static methods.
The functionality of this class can be
- Get a client
- Get ids in several ways
- Get a record in several ways.
1. Implementation of the class
The name of the class may be "ORVEUtils" and this can be the code proposals
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | package ximodante.orve; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.ws.Holder; import org.apache.commons.lang3.reflect.FieldUtils; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.module.jaxb.JaxbAnnotationModule; import ximodante.sicres.multiclass.FicheroIntercambioSICRES3; import ximodante.utils.ObjectBuilder; /** * Utility class to manage ORVE WS. * * @author Ximo Dante * * */ public class ORVEUtils { /** * Get a dynamic client from the URL of the WSDL * @param wsdlURL * @return */ public static Client getClient(String wsdlURL) { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); return dcf.createClient(wsdlURL); } /** * Return a Jackson XML Mapper * @param defaultUserWrapper for accepting collections wrappers (For ORVE set to false) * @param acceptJaxbAnnotations for accepting Jaxb annotations (Fort ORVE set to true) * @return */ public static XmlMapper getXmlMapper(boolean defaultUserWrapper, boolean acceptJaxbAnnotations) { XmlMapper mapper = new XmlMapper(); mapper.setDefaultUseWrapper(defaultUserWrapper); if (acceptJaxbAnnotations) mapper.registerModule(new JaxbAnnotationModule()); // Accept JAXB Annotations !!!! return mapper; } /** * Return a Jackson XML Mapper suitable for ORVE that: * doesn't accept collections wrappers * accepts Jaxb annotations * @return */ public static XmlMapper getXmlMapper() { return getXmlMapper(false, true); } /** * Get a list of ids of record calling the "obtenerIdentificadores" operation from the WS * @param client * @param security * @param mapParams * @return * @throws Exception */ public static List<Integer> getRecordIds(Client client, Object security, Map<String,Object> mapParams) throws Exception { //1.1 Holder<Security> var<Object> secHolder = new Holder<Object>(security); //2.1 Filtros identificadores var filtros=ObjectBuilder.getObject("https.ssweb_seap_minhap_es.demoorve.FiltrosIdentificadores", mapParams); //3 Invoke WS operation // 3.1 Output object (obtained by invoking the WS operation) Object[] res=null; // 3.2 Operation name from the WSDL String operationWS="obtenerIdentificadores"; // 3.3 WS invocation with operation name and input structure res = client.invoke(operationWS,secHolder,filtros); var b=res[1]; //Holder<obtenerIdentificaoresRespuestaWS> var value= FieldUtils.readField(b, "value", true); //obtenerIdentificaoresRespuestaWS String code=(String)(FieldUtils.readField(value, "codigo", true)); String description=(String)(FieldUtils.readField(value, "descripcion", true)); if (! code.equals("00") ) throw (new Exception("Error " + code + ": " + description)); else { var aIden=FieldUtils.readField(value, "identificadores", true); return (List<Integer>) FieldUtils.readField(aIden, "item", true); } } /** * Get a list of ids of record calling the "obtenerIdentificadores" operation from the WS * @param client * @param user * @param password * @param mapParams * @return * @throws Exception */ public static List<Integer> getRecordIds(Client client, String user, String password, Map<String,Object> mapParams) throws Exception { //1. Create security object var security = getSecurity (user, password); //2. Call previous method return getRecordIds(client, security, mapParams); } /** * Get a list of ids of record calling the "obtenerIdentificadores" operation from the WS * @param client * @param security * @param state * @param DIR3Admin * @param DIR3Office * @param dateFrom * @param dateTo * @return * @throws Exception */ public static List<Integer> getRecordIds(Client client, Object security, String state, String DIR3Admin, String DIR3Office, String dateFrom, String dateTo) throws Exception { var mapParams= new HashMap<String,Object>(); mapParams.put("estado", state); mapParams.put("fechaInicio", dateFrom); mapParams.put("fechaFin", dateTo); if (DIR3Admin !=null) mapParams.put("unidad", DIR3Admin); //Catarroja if (DIR3Office!=null) mapParams.put("oficina", DIR3Office);// Of. Registro de Catarroja return getRecordIds(client, security, mapParams); } /** * Get a list of ids of record calling the "obtenerIdentificadores" operation from the WS * @param client * @param user * @param password * @param state * @param DIR3Admin * @param DIR3Office * @param dateFrom * @param dateTo * @return * @throws Exception */ public static List<Integer> getRecordIds(Client client, String user, String password, String state, String DIR3Admin, String DIR3Office, String dateFrom, String dateTo) throws Exception { //1. Create security object var security = getSecurity (user, password); return getRecordIds(client, security, state, DIR3Admin, DIR3Office, dateFrom, dateTo); } /** * Get an instance of https.ssweb_seap_minhap_es.demoorve.Security class * @param user * @param password * @return * @throws ClassNotFoundException * @throws InstantiationException * @throws IllegalAccessException * @throws IllegalArgumentException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws SecurityException */ private static Object getSecurity (String user, String password) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { var<String, Object> secMap = new HashMap<String, Object>(); secMap.put("username",user); secMap.put("password", password); return ObjectBuilder.getObject("https.ssweb_seap_minhap_es.demoorve.Security", secMap); } /** * Get an XML String representing a FicheroIntercambioSICRES3 instance calling the "obtenerRegistro" operation from the WS * @param client * @param security * @param id * @return * @throws Exception */ public static String getRegistro (Client client, Object security, int id) throws Exception { Object[] reg = client.invoke("obtenerRegistro",security,id); String code = (String)(FieldUtils.readField( reg[1], "codigo", true)); String description = (String)(FieldUtils.readField( reg[1], "descripcion", true)); String record =(String)(FieldUtils.readField( reg[1], "registro", true)); if (! code.equals("00") ) throw (new Exception("Error " + code + ": " + description)); else return record; } /** * Get an XML String representing a FicheroIntercambioSICRES3 instance calling the "obtenerRegistro" operation from the WS * @param client * @param user * @param password * @param id * @return * @throws Exception */ public static String getRegistro (Client client, String user, String password, int id) throws Exception { //1. Create security object var security = getSecurity (user, password); return getRegistro(client, security, id); } /** * Get a FicheroIntercambioSICRES3 instance from an XML String * @param mapper * @param xmlString * @return * @throws JsonParseException * @throws JsonMappingException * @throws IOException */ public static FicheroIntercambioSICRES3 getRegistroSicres3 (XmlMapper mapper, String xmlString) throws JsonParseException, JsonMappingException, IOException { return mapper.readValue(xmlString, FicheroIntercambioSICRES3.class); } /** * Get a FicheroIntercambioSICRES3 instance by invoking the "obtenerRegistro" operation from the WS * @param client * @param security * @param id * @param mapper * @return * @throws JsonParseException * @throws JsonMappingException * @throws IOException * @throws Exception */ public static FicheroIntercambioSICRES3 getRegistroSicres3 (Client client, Object security, int id, XmlMapper mapper) throws JsonParseException, JsonMappingException, IOException, Exception { return getRegistroSicres3(mapper, getRegistro (client, security, id)); } /** * Get a FicheroIntercambioSICRES3 instance by invoking the "obtenerRegistro" operation from the WS * @param client * @param user * @param password * @param id * @param mapper * @return * @throws JsonParseException * @throws JsonMappingException * @throws IOException * @throws Exception */ public static FicheroIntercambioSICRES3 getRegistroSicres3 (Client client, String user, String password, int id, XmlMapper mapper) throws JsonParseException, JsonMappingException, IOException, Exception { return getRegistroSicres3(mapper, getRegistro (client, user, password, id)); } public static void main(String[] args) { //1. Test getClient var client=getClient("https://ssweb.seap.minhap.es/demoorve/WSExportacion.wsdl"); //2. Test getXmlMapper var mapper=getXmlMapper(); //3. params var user="myUser"; var password="myPassword"; var state="EC"; var DIR3Admin="LA1000323"; var dateFrom="2018-09-01 00:00:01"; var dateTo="2018-09-25 23:59:59"; //4. Get a list of ids to retrieve records try { var lstIds=getRecordIds(client, user, password, state, DIR3Admin, null, dateFrom, dateTo); for (int id: lstIds) { var sicres =getRegistroSicres3(client, user, password, id, mapper); System.out.println(id); System.out.println(" " + sicres.getDeAsunto().getResumen()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("That's all Folks!"); } } |
Comments
Post a Comment