From 4c3cc396ff4769b7dbdab2ea30376e1e880e9d0d Mon Sep 17 00:00:00 2001 From: Andreas Rudolph Date: Sat, 25 Apr 2015 13:25:57 +0200 Subject: [PATCH] separated updating & publishing of objects in bulk exports During bulk exports a real estate is immediately published after an insert / update. Publishing takes place before any removals are processed. This may lead to problems, when the IS24 account has a fixed amount of publishable objects. The fixed limit may temporarily exceed and new objects are not properly published due to an error response by the webservice. In order to fix that problem we've separated updating and publishing of real estates into two separate functions. Pulishing of real estates takes place not until any inserts, updates and removals were processed. --- .../is24/restapi/utils/ExportHandler.java | 236 ++++++++++++------ 1 file changed, 164 insertions(+), 72 deletions(-) diff --git a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/ExportHandler.java b/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/ExportHandler.java index fe4da70..37cb772 100644 --- a/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/ExportHandler.java +++ b/OpenEstate-IS24-REST/src/main/java/org/openestate/is24/restapi/utils/ExportHandler.java @@ -552,6 +552,112 @@ protected PublishChannels doLoadPublishChannels() throws IOException } } + /** + * Publishes a real estate at the Webservice. + * + * @param is24ObjectId + * real estate ID by IS24 + * + * @param externalObjectId + * external real estate ID + * + * @param is24PublishChannels + * channels, where the real estate should be published + * + * @throws IOException + * if the operation failed + */ + protected void doPublishObject( long is24ObjectId, String externalObjectId, PublishChannels is24PublishChannels ) throws IOException + { + final org.openestate.is24.restapi.xml.common.ObjectFactory commonFactory = + new org.openestate.is24.restapi.xml.common.ObjectFactory(); + + try + { + // derzeitige Veröffentlichungen zur Immobilie ermitteln + final List is24PublishedChannels = new ArrayList(); + try + { + PublishObjects is24Publishings = ImportExport.PublishService.get( this.client, is24ObjectId, 0 ); + if (is24Publishings!=null) + { + for (PublishObject is24Publishing : is24Publishings.getPublishObject()) + { + is24PublishedChannels.add( is24Publishing.getPublishChannel().getId() ); + } + } + } + catch (RequestFailedException ex) + { + LOGGER.error( "Can't get publishings of property '" + externalObjectId + "' (" + is24ObjectId + ") from the Webservice!" ); + if (ex.requestRefNumber!=null) LOGGER.error( "> referring request: " + ex.requestRefNumber ); + logMessagesAsError( ex.responseMessages ); + LOGGER.error( "> " + ex.getLocalizedMessage(), ex ); + this.putObjectMessage( + externalObjectId, ExportMessage.Code.OBJECT_PUBLISHINGS_NOT_FOUND, ex ); + } + + if (is24PublishChannels==null) + { + this.putObjectMessage( + externalObjectId, ExportMessage.Code.OBJECT_NOT_PUBLISHED, + "No channels for publishing found!" ); + } + else + { + // Veröffentlichungen zur Immobilie aktualisieren, + // wenn diese zu einem Kanal noch nicht gesetzt wurde + for (PublishChannel is24Channel : is24PublishChannels.getPublishChannel()) + { + Long is24ChannelId = is24Channel.getId(); + if (is24PublishedChannels.contains( is24ChannelId )) + continue; + + PublishObject is24Publishing = commonFactory.createPublishObject(); + is24Publishing.setPublishChannel( is24Channel ); + is24Publishing.setRealEstate( commonFactory.createPublishObjectRealEstate() ); + is24Publishing.getRealEstate().setId( is24ObjectId ); + + try + { + ImportExport.PublishService.post( this.client, is24Publishing ); + } + catch (RequestFailedException ex) + { + LOGGER.error( "Can't publish property '" + externalObjectId + "' (" + is24ObjectId + ") " + + "in channel '" + is24Channel.getTitle() + "' (" + is24ChannelId + ")!" ); + if (ex.requestRefNumber!=null) LOGGER.error( "> referring request: " + ex.requestRefNumber ); + logMessagesAsError( ex.responseMessages ); + LOGGER.error( "> " + ex.getLocalizedMessage(), ex ); + this.putObjectMessage( + externalObjectId, ExportMessage.Code.OBJECT_NOT_PUBLISHED, ex ); + } + } + } + } + catch (JAXBException ex) + { + //LOGGER.error( "Can't read / write XML while communicating with the Webservice!" ); + //LOGGER.error( "> " + ex.getLocalizedMessage(), ex ); + throw new IOExceptionWithCause( + "Can't read / write XML while communicating with the Webservice!", ex ); + } + catch (OAuthException ex) + { + //LOGGER.error( "Can't authorize at the Webservice!" ); + //LOGGER.error( "> " + ex.getLocalizedMessage(), ex ); + throw new IOExceptionWithCause( + "Authorization failed!", ex ); + } + catch (IOException ex) + { + //LOGGER.error( "Can't communicate with the Webservice!" ); + //LOGGER.error( "> " + ex.getLocalizedMessage(), ex ); + throw new IOExceptionWithCause( + "Communication failed!", ex ); + } + } + /** * Removes a real estate object from the Webservice. * @@ -800,9 +906,6 @@ protected Long doUpdateContact( RealtorContactDetails contact, String poolContac * @param object * real estate to save * - * @param is24PublishChannels - * channels, where the real estate should be published - * * @param poolObjectId * real estate ID within export pool * @@ -813,11 +916,9 @@ protected Long doUpdateContact( RealtorContactDetails contact, String poolContac * @throws IOException * if the operation failed */ - protected Long doUpdateObject( RealEstate object, PublishChannels is24PublishChannels, String poolObjectId ) throws IOException + protected Long doUpdateObject( RealEstate object, String poolObjectId ) throws IOException { final String externalObjectId = object.getExternalId(); - final org.openestate.is24.restapi.xml.common.ObjectFactory commonFactory = - new org.openestate.is24.restapi.xml.common.ObjectFactory(); final org.openestate.is24.restapi.xml.realestates.ObjectFactory realEstatesFactory = new org.openestate.is24.restapi.xml.realestates.ObjectFactory(); final org.openestate.is24.restapi.xml.attachmentsorder.ObjectFactory attachmentsorderFactory = @@ -1401,67 +1502,6 @@ else if (attachFileName.toLowerCase().endsWith( ".gif" )) } } - // derzeitige Veröffentlichungen zur Immobilie ermitteln - final List is24PublishedChannels = new ArrayList(); - try - { - PublishObjects is24Publishings = ImportExport.PublishService.get( this.client, is24ObjectId, 0 ); - if (is24Publishings!=null) - { - for (PublishObject is24Publishing : is24Publishings.getPublishObject()) - { - is24PublishedChannels.add( is24Publishing.getPublishChannel().getId() ); - } - } - } - catch (RequestFailedException ex) - { - LOGGER.error( "Can't get publishings of property '" + externalObjectId + "' (" + is24ObjectId + ") from the Webservice!" ); - if (ex.requestRefNumber!=null) LOGGER.error( "> referring request: " + ex.requestRefNumber ); - logMessagesAsError( ex.responseMessages ); - LOGGER.error( "> " + ex.getLocalizedMessage(), ex ); - this.putObjectMessage( - externalObjectId, ExportMessage.Code.OBJECT_PUBLISHINGS_NOT_FOUND, ex ); - } - - if (is24PublishChannels==null) - { - this.putObjectMessage( - externalObjectId, ExportMessage.Code.OBJECT_NOT_PUBLISHED, - "No channels for publishing found!" ); - } - else - { - // Veröffentlichungen zur Immobilie aktualisieren, - // wenn diese zu einem Kanal noch nicht gesetzt wurde - for (PublishChannel is24Channel : is24PublishChannels.getPublishChannel()) - { - Long is24ChannelId = is24Channel.getId(); - if (is24PublishedChannels.contains( is24ChannelId )) - continue; - - PublishObject is24Publishing = commonFactory.createPublishObject(); - is24Publishing.setPublishChannel( is24Channel ); - is24Publishing.setRealEstate( commonFactory.createPublishObjectRealEstate() ); - is24Publishing.getRealEstate().setId( is24ObjectId ); - - try - { - ImportExport.PublishService.post( this.client, is24Publishing ); - } - catch (RequestFailedException ex) - { - LOGGER.error( "Can't publish property '" + externalObjectId + "' (" + is24ObjectId + ") " - + "in channel '" + is24Channel.getTitle() + "' (" + is24ChannelId + ")!" ); - if (ex.requestRefNumber!=null) LOGGER.error( "> referring request: " + ex.requestRefNumber ); - logMessagesAsError( ex.responseMessages ); - LOGGER.error( "> " + ex.getLocalizedMessage(), ex ); - this.putObjectMessage( - externalObjectId, ExportMessage.Code.OBJECT_NOT_PUBLISHED, ex ); - } - } - } - return is24ObjectId; } catch (JAXBException ex) @@ -1487,6 +1527,36 @@ else if (attachFileName.toLowerCase().endsWith( ".gif" )) } } + /** + * Saves a real estate to the Webservice. + * + * @param object + * real estate to save + * + * @param is24PublishChannels + * channels, where the real estate should be published + * + * @param poolObjectId + * real estate ID within export pool + * + * @return + * ID of the processed real estate in the Webservice or null, if the object + * was not updated + * + * @throws IOException + * if the operation failed + * + * @deprecated + * This method was splitted into {@link #doUpdateObject(org.openestate.is24.restapi.xml.realestates.RealEstate, java.lang.String)} + * and {@link #doPublishObject(long, java.lang.String, org.openestate.is24.restapi.xml.common.PublishChannels)}. + * It will be removed with the next major update. + */ + @Deprecated + protected Long doUpdateObject( RealEstate object, PublishChannels is24PublishChannels, String poolObjectId ) throws IOException + { + return doUpdateObject( object, poolObjectId ); + } + /** * Starts the export of an {@link ExportPool}. * @@ -1521,11 +1591,8 @@ public ExportMessage[] export( AbstractClient client, ExportPool pool, boolean d this.totalProgress = this.pool.getTotalSize(); this.setProgress( 0 ); - // load available publish channels - LOGGER.info( "loading publish channels" ); - final PublishChannels publishChannels = doLoadPublishChannels(); - // updating contacts + Map is24ObjectIds = new HashMap(); String[] ids = this.pool.getContactIds(); if (!ArrayUtils.isEmpty( ids )) { @@ -1601,7 +1668,11 @@ else if (this.canIgnoreObject( object, poolObjectId )) { LOGGER.info( "[" + counter + " / " + ids.length +"] " + "updating object '" + object.getExternalId() + "'" ); - this.doUpdateObject( object, publishChannels, poolObjectId ); + Long is24ObjectId = this.doUpdateObject( object, poolObjectId ); + if (is24ObjectId!=null) + { + is24ObjectIds.put( is24ObjectId, StringUtils.trimToNull( object.getExternalId() ) ); + } } } } @@ -1657,6 +1728,27 @@ else if (this.canIgnoreObject( object, poolObjectId )) } } + if (!is24ObjectIds.isEmpty()) + { + // load available publish channels + LOGGER.info( "loading publish channels" ); + final PublishChannels publishChannels = doLoadPublishChannels(); + + // publishing objects + LOGGER.info( "publishing objects" ); + int counter = 0; + for (Map.Entry entry : is24ObjectIds.entrySet()) + { + counter++; + Long is24ObjectId = entry.getKey(); + String externalObjectId = entry.getValue(); + + LOGGER.info( "[" + counter + " / " + is24ObjectIds.size() +"] " + + "publishing object '" + externalObjectId + "' (" + is24ObjectId + ")" ); + doPublishObject( is24ObjectId, externalObjectId, publishChannels ); + } + } + return this.getMessages(); }