Author: jacopoc
Date: Mon Jul 9 03:14:05 2007 New Revision: 554593 URL: http://svn.apache.org/viewvc?view=rev&rev=554593 Log: Applied patch by Marco Risaliti (OFBIZ-1095) with further improvements to the Google export interface. Modified: ofbiz/trunk/applications/product/config/ProductUiLabels.properties ofbiz/trunk/applications/product/config/ProductUiLabels_it.properties ofbiz/trunk/applications/product/servicedef/services.xml ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductSearchEvents.java ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductsExportToGoogle.java ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/controller.xml ofbiz/trunk/applications/product/webapp/catalog/find/ExportForms.xml Modified: ofbiz/trunk/applications/product/config/ProductUiLabels.properties URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/config/ProductUiLabels.properties?view=diff&rev=554593&r1=554592&r2=554593 ============================================================================== --- ofbiz/trunk/applications/product/config/ProductUiLabels.properties (original) +++ ofbiz/trunk/applications/product/config/ProductUiLabels.properties Mon Jul 9 03:14:05 2007 @@ -557,6 +557,9 @@ ProductExpireDate=Expire Date ProductExpireResultsFrom=Expire Results From ProductExplodeOrderItems=Explode Order Items +ProductExportDeactivated=Deactivated +ProductExportPublish=Publish +ProductExportPublishDraft=Publish Draft ProductExportToGoogle=Export To Google Base ProductExportNoTrackingRequested=Not Requested ProductFacilities=Facilities @@ -1481,8 +1484,10 @@ productevents.thru_date_not_formatted_correctly=Thru Date not formatted correctly. productevents.updatemode_not_specified=Update Mode was not specified, but is required. productsExportToGoogle.errorDuringAuthenticationToGoogle=Error during authentication to Google Account +productsExportToGoogle.errorInTheResponseFromGoogle=Error in the response from Google : productsExportToGoogle.exceptionDuringBuildingDataItemsToGoogle=Exception occurred during building data items to Google Base productsExportToGoogle.exceptionInExportToGoogle=Exception in exportToGoogle +productsExportToGoogle.exceptionReadingResponseFromGoogle=Exception reading response from Google productsExportToGoogle.productItemsSentCorrecltyToGoogle=Product items sent correclty to Google Base productsearchevents.added_x_product_category_members=Added ${numAdded} product category members. productsearchevents.error_getting_search_results=Error getting search results: ${errSearchResult}. @@ -1491,6 +1496,7 @@ productsearchevents.no_results_found_probably_error_constraints=No results found, probably because there was an error or were no constraints. productsearchevents.removed_x_items=removed ${numRemoved} items. productsearchevents.thruDate_not_formatted_properly=The thruDate is not formatted properly: ${errDateFormat}. +productsearchevents.exceptionCallingExportToGoogle=Exception calling exportToGoogle productservices.both_productId_and_productIdTo_cannot_be_defined=Both productId and productIdTo cannot be defined. productservices.both_productId_and_productIdTo_cannot_be_null=Both productId and productIdTo cannot be null. productservices.cannot_build_feature_tree_full=Cannot build feature tree: featureList is null. @@ -1866,4 +1872,5 @@ FormFieldTitle_monthsInPastLimit=Months In Past Limit FormFieldTitle_picklistBinId=Picklist Bin FormFieldTitle_webSiteUrl=Web Site Url -FormFieldTitle_trackingCodeId=Tracking Code Id \ No newline at end of file +FormFieldTitle_trackingCodeId=Tracking Code Id +FormFieldTitle_actionType=Action Type Modified: ofbiz/trunk/applications/product/config/ProductUiLabels_it.properties URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/config/ProductUiLabels_it.properties?view=diff&rev=554593&r1=554592&r2=554593 ============================================================================== --- ofbiz/trunk/applications/product/config/ProductUiLabels_it.properties (original) +++ ofbiz/trunk/applications/product/config/ProductUiLabels_it.properties Mon Jul 9 03:14:05 2007 @@ -1467,8 +1467,10 @@ productevents.thru_date_not_formatted_correctly=Data A non formattata correttamente. productevents.updatemode_not_specified=Modo di Aggiornamento non \u00e8 stato specificato, ma \u00e8 richiesto. productsExportToGoogle.errorDuringAuthenticationToGoogle=Errore durante l\'autenticazione su Google Account +productsExportToGoogle.errorInTheResponseFromGoogle=Errore nella risposta di Google : productsExportToGoogle.exceptionDuringBuildingDataItemsToGoogle=Eccezione accaduta durante la creazione dei dati prodotti per Google Base productsExportToGoogle.exceptionInExportToGoogle=Eccezione in exportToGoogle +productsExportToGoogle.exceptionReadingResponseFromGoogle=Eccezione leggendo la risposta da Google productsExportToGoogle.productItemsSentCorrecltyToGoogle=Prodotti inviati correttamente su Google Base productsearchevents.added_x_product_category_members=Aggiunti ${numAdded} membri prodotto categoria. productsearchevents.error_getting_search_results=Errore leggendo i risultati di ricerca: ${errSearchResult}. @@ -1477,6 +1479,7 @@ productsearchevents.no_results_found_probably_error_constraints=Nessun risultato trovato, probabilmente perch\u00e9 c'era un'errore o nessuna costrizione. productsearchevents.removed_x_items=${numRemoved} righe cancellate. productsearchevents.thruDate_not_formatted_properly=La Data A non \u00e8 stata formattata propriamente: ${errDateFormat}. +productsearchevents.exceptionCallingExportToGoogle=Eccezione durante l'esecuzione di exportToGoogle productservices.both_productId_and_productIdTo_cannot_be_defined=Entrambi productId e productIdTo non sono stati definiti. productservices.both_productId_and_productIdTo_cannot_be_null=Entrambi productId e productIdTo non possono essere nulli. productservices.cannot_build_feature_tree_full=Non \u00e8 possibli costruire l'albero delle caratteristiche build feature tree: featureList \u00e8 nulla. @@ -1853,3 +1856,4 @@ FormFieldTitle_picklistBinId=Lista di Prelievo FormFieldTitle_webSiteUrl=Url Sito Web FormFieldTitle_trackingCodeId=Codice Tracciatura +FormFieldTitle_actionType=Tipo Azione Modified: ofbiz/trunk/applications/product/servicedef/services.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/servicedef/services.xml?view=diff&rev=554593&r1=554592&r2=554593 ============================================================================== --- ofbiz/trunk/applications/product/servicedef/services.xml (original) +++ ofbiz/trunk/applications/product/servicedef/services.xml Mon Jul 9 03:14:05 2007 @@ -1126,4 +1126,14 @@ <attribute type="String" mode="IN" name="statusId" optional="false"/> <attribute type="String" mode="IN" name="trackingCodeId" optional="true"/> </service> + <service name="searchExportProductListToGoogle" engine="java" + location="org.ofbiz.product.product.ProductSearchEvents" invoke="searchExportProductListToGoogle" auth="true"> + <description>Export products to Google Base</description> + <attribute type="String" mode="IN" name="webSiteUrl" optional="false"/> + <attribute type="String" mode="IN" name="imageUrl" optional="false"/> + <attribute type="String" mode="IN" name="actionType" optional="false"/> + <attribute type="String" mode="IN" name="statusId" optional="false"/> + <attribute type="List" mode="IN" name="selectResult" optional="true"/> + <attribute type="String" mode="IN" name="trackingCodeId" optional="true"/> + </service> </services> Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductSearchEvents.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductSearchEvents.java?view=diff&rev=554593&r1=554592&r2=554593 ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductSearchEvents.java (original) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductSearchEvents.java Mon Jul 9 03:14:05 2007 @@ -45,6 +45,10 @@ import org.ofbiz.entity.util.EntityUtil; import org.ofbiz.product.product.ProductSearch.ProductSearchContext; import org.ofbiz.product.product.ProductSearch.ResultSortOrder; +import org.ofbiz.service.DispatchContext; +import org.ofbiz.service.GenericServiceException; +import org.ofbiz.service.LocalDispatcher; +import org.ofbiz.service.ServiceUtil; /** * Product Search Related Events @@ -448,6 +452,57 @@ request.setAttribute("productExportList", productExportList); return "success"; + } + + public static Map searchExportProductListToGoogle(DispatchContext dctx, Map context) { + LocalDispatcher dispatcher = dctx.getDispatcher(); + HttpServletRequest request = (HttpServletRequest) context.get("request"); + Locale locale = (Locale)context.get("locale"); + List productExportList = (List) context.get("selectResult"); + + try { + boolean beganTransaction = TransactionUtil.begin(DEFAULT_TX_TIMEOUT); + try { + if (UtilValidate.isEmpty(productExportList)) { + // If the passed list of product ids is empty, get the list from the search parameters in the request + EntityListIterator eli = getProductSearchResults(request); + if (eli == null) { + return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "productsearchevents.no_results_found_probably_error_constraints", locale)); + } + + GenericValue searchResultView = null; + while ((searchResultView = (GenericValue) eli.next()) != null) { + productExportList.add(searchResultView.getString("mainProductId")); + } + eli.close(); + } + String webSiteUrl = (String)context.get("webSiteUrl"); + String imageUrl = (String)context.get("imageUrl"); + String actionType = (String)context.get("actionType"); + String statusId = (String)context.get("statusId"); + String trackingCodeId = (String)context.get("trackingCodeId"); + + // Export all the products to Google Base + try { + Map exportResult = dispatcher.runSync("exportProductsToGoogle", UtilMisc.toMap("selectResult", productExportList, "webSiteUrl", webSiteUrl, + "imageUrl", imageUrl, "actionType", actionType, "statusId", statusId, "trackingCodeId", trackingCodeId)); + if (ServiceUtil.isError(exportResult)) { + return ServiceUtil.returnError(ServiceUtil.getErrorMessage(exportResult)); + } + } catch (GenericServiceException e) { + Debug.logError(e, "Exception calling exportToGoogle", module); + return ServiceUtil.returnError(UtilProperties.getMessage(resource, "productsearchevents.exceptionCallingExportToGoogle", locale)); + } + } catch (GenericEntityException e) { + return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "productsearchevents.error_getting_search_results", locale)); + } finally { + TransactionUtil.commit(beganTransaction); + } + } catch (GenericTransactionException e) { + return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "productsearchevents.error_getting_search_results", locale)); + } + + return ServiceUtil.returnSuccess(); } public static EntityListIterator getProductSearchResults(HttpServletRequest request) { Modified: ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductsExportToGoogle.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductsExportToGoogle.java?view=diff&rev=554593&r1=554592&r2=554593 ============================================================================== --- ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductsExportToGoogle.java (original) +++ ofbiz/trunk/applications/product/src/org/ofbiz/product/product/ProductsExportToGoogle.java Mon Jul 9 03:14:05 2007 @@ -19,7 +19,6 @@ package org.ofbiz.product.product; import java.io.BufferedReader; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -32,15 +31,10 @@ import java.util.Locale; import java.util.Map; import java.util.StringTokenizer; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import javolution.util.FastMap; -import org.apache.xml.serialize.OutputFormat; -import org.apache.xml.serialize.XMLSerializer; import org.ofbiz.base.util.Debug; -import org.ofbiz.base.util.StringUtil; import org.ofbiz.base.util.UtilFormatOut; import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilProperties; @@ -86,11 +80,12 @@ Map result = buildDataItemsXml(dctx, context, dataItemsXml); if (!ServiceUtil.isFailure(result)) { String token = authenticate(authenticationUrl, accountEmail, accountPassword); - if (token != null) { - result = postItem(token, postItemsUrl, developerKey, dataItemsXml); - if (ServiceUtil.isFailure(result)) - return ServiceUtil.returnFailure(ServiceUtil.getErrorMessage(result)); + result = postItem(token, postItemsUrl, developerKey, dataItemsXml, locale); + String msg = ServiceUtil.getErrorMessage(result); + if (msg != null && msg.length() > 0) { + return ServiceUtil.returnFailure(msg); + } } else { Debug.logError("Error during authentication to Google Account", module); return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "productsExportToGoogle.errorDuringAuthenticationToGoogle", locale)); @@ -128,7 +123,7 @@ StringTokenizer tokenizer = new StringTokenizer(postOutput, "=\n "); while (tokenizer.hasMoreElements()) { - if (tokenizer.nextToken().equals("Auth")) { + if ("Auth".equals(tokenizer.nextToken())) { if (tokenizer.hasMoreElements()) { token = tokenizer.nextToken(); } @@ -188,7 +183,7 @@ return outputBuilder.toString(); } - private static Map postItem(String token, String postItemsUrl, String developerKey, StringBuffer dataItems) throws IOException { + private static Map postItem(String token, String postItemsUrl, String developerKey, StringBuffer dataItems, Locale locale) throws IOException { HttpURLConnection connection = (HttpURLConnection)(new URL(postItemsUrl)).openConnection(); connection.setDoInput(true); @@ -205,12 +200,18 @@ int responseCode = connection.getResponseCode(); InputStream inputStream; Map result = FastMap.newInstance(); - if (responseCode == HttpURLConnection.HTTP_CREATED) { - inputStream = connection.getInputStream(); - result = ServiceUtil.returnSuccess(toString(inputStream)); - } else if (responseCode == HttpURLConnection.HTTP_OK) { + if (responseCode == HttpURLConnection.HTTP_CREATED || responseCode == HttpURLConnection.HTTP_OK) { inputStream = connection.getInputStream(); - result = ServiceUtil.returnSuccess(toString(inputStream)); + String response = toString(inputStream); + if (response != null && response.length() > 0) { + result = readResponseFromGoogle(response, locale); + String msg = ServiceUtil.getErrorMessage(result); + if (msg != null && msg.length() > 0) { + return ServiceUtil.returnFailure(msg); + } else { + return ServiceUtil.returnSuccess(); + } + } } else { inputStream = connection.getErrorStream(); result = ServiceUtil.returnFailure(toString(inputStream)); @@ -269,6 +270,18 @@ Element batchElem = UtilXml.addChildElement(entryElem, "batch:operation", feedDocument); batchElem.setAttribute("type", actionType); + // status is draft or deactivate + if (statusId != null && ("draft".equals(statusId) || "deactivate".equals(statusId))) { + Element appControlElem = UtilXml.addChildElement(entryElem, "app:control", feedDocument); + appControlElem.setAttribute("xmlns:app", "http://purl.org/atom/app#"); + UtilXml.addChildElementValue(appControlElem, "app:draft", "yes", feedDocument); + + // status is deactivate + if ("deactivate".equals(statusId)) { + UtilXml.addChildElement(appControlElem, "gm:disapproved", feedDocument); + } + } + UtilXml.addChildElementValue(entryElem, "title", title, feedDocument); Element contentElem = UtilXml.addChildElementValue(entryElem, "content", description, feedDocument); @@ -284,13 +297,10 @@ UtilXml.addChildElementValue(entryElem, "g:item_type", "products", feedDocument); UtilXml.addChildElementValue(entryElem, "g:price", price, feedDocument); + // if the product has an image it will be published on Google Product Search if (UtilValidate.isNotEmpty(image_link)) { UtilXml.addChildElementValue(entryElem, "g:image_link", image_link, feedDocument); } - - Element appControlElem = UtilXml.addChildElement(entryElem, "app:control", feedDocument); - appControlElem.setAttribute("xmlns:app", "http://purl.org/atom/app#"); - UtilXml.addChildElementValue(appControlElem, "app:draft", "yes", feedDocument); } dataItemsXml.append(UtilXml.writeXmlDocument(feedDocument)); @@ -318,5 +328,35 @@ Debug.logError("Exception calculating price for product [" + product.getString("productId") + "]", module); } return priceString; + } + + private static Map readResponseFromGoogle(String msg, Locale locale) { + StringBuffer message = new StringBuffer(); + try { + Document docResponse = UtilXml.readXmlDocument(msg, true); + Element elemResponse = docResponse.getDocumentElement(); + List atomEntryList = UtilXml.childElementList(elemResponse, "atom:entry"); + Iterator atomEntryElemIter = atomEntryList.iterator(); + while (atomEntryElemIter.hasNext()) { + Element atomEntryElement = (Element)atomEntryElemIter.next(); + List batchInterruptedEntryList = UtilXml.childElementList(atomEntryElement, "batch:interrupted"); + Iterator batchInterruptedEntryElemIter = batchInterruptedEntryList.iterator(); + while (batchInterruptedEntryElemIter.hasNext()) { + Element batchInterruptedEntryElement = (Element)batchInterruptedEntryElemIter.next(); + String reason = batchInterruptedEntryElement.getAttribute("reason"); + message.append(reason); + } + } + } catch (Exception e) { + Debug.logError("Exception reading response from Google", module); + return ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "productsExportToGoogle.exceptionReadingResponseFromGoogle", locale)); + } + + if (message.length() > 0) { + Debug.logError("Error in the response from Google " + message.toString(), module); + message.insert(0, UtilProperties.getMessage(resource, "productsExportToGoogle.errorInTheResponseFromGoogle", locale)); + return ServiceUtil.returnFailure(message.toString()); + } + return ServiceUtil.returnSuccess(); } } Modified: ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/controller.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/controller.xml?view=diff&rev=554593&r1=554592&r2=554593 ============================================================================== --- ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/controller.xml (original) +++ ofbiz/trunk/applications/product/webapp/catalog/WEB-INF/controller.xml Mon Jul 9 03:14:05 2007 @@ -2385,6 +2385,13 @@ <response name="error" type="view" value="ProductsExportToGoogle"/> </request-map> + <request-map uri="searchExportProductListToGoogle"> + <security https="true" auth="true"/> + <event type="service" path="" invoke="searchExportProductListToGoogle"/> + <response name="success" type="view" value="ProductsExportToGoogle"/> + <response name="error" type="view" value="ProductsExportToGoogle"/> + </request-map> + <!-- end of request mappings --> <!-- View Mappings --> Modified: ofbiz/trunk/applications/product/webapp/catalog/find/ExportForms.xml URL: http://svn.apache.org/viewvc/ofbiz/trunk/applications/product/webapp/catalog/find/ExportForms.xml?view=diff&rev=554593&r1=554592&r2=554593 ============================================================================== --- ofbiz/trunk/applications/product/webapp/catalog/find/ExportForms.xml (original) +++ ofbiz/trunk/applications/product/webapp/catalog/find/ExportForms.xml Mon Jul 9 03:14:05 2007 @@ -20,7 +20,7 @@ <forms xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.ofbiz.org/dtds/widget-form.xsd"> - <form name="ProductsExportToGoogle" type="single" target="exportProductsToGoogle"> + <form name="ProductsExportToGoogle" type="single" target="searchExportProductListToGoogle"> <field name="selectResult"><hidden/></field> <field name="webSiteUrl"><text size="50" maxlength="250"/></field> <field name="imageUrl"><text size="50" maxlength="250"/></field> @@ -33,14 +33,16 @@ --> </drop-down> </field> - <field name="statusId" widget-style="selectBox"> - <drop-down no-current-selected-key="deactivate"> + <field name="statusId"> + <drop-down no-current-selected-key="draft"> + <!-- <option key="deactivate" description="${uiLabelMap.ProductExportDeactivated}"/> + --> <option key="draft" description="${uiLabelMap.ProductExportPublishDraft}"/> <option key="publish" description="${uiLabelMap.ProductExportPublish}"/> </drop-down> </field> - <field name="trackingCodeId" widget-style="selectBox"> + <field name="trackingCodeId"> <drop-down no-current-selected-key="_NA_"> <option key="_NA_" description="${uiLabelMap.ProductExportNoTrackingRequested}"/> <entity-options entity-name="TrackingCode" description="${description}"> |
Free forum by Nabble | Edit this page |