<?xml version="1.0" encoding="UTF-8" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<simple-methods xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://ofbiz.apache.org/Simple-Method" xsi:schemaLocation="http://ofbiz.apache.org/Simple-Method http://ofbiz.apache.org/dtds/simple-methods.xsd">
    <!-- ProductContent -->
    <simple-method method-name="createProductContent" short-description="Create Content For Product">
        <make-value entity-name="ProductContent" value-field="newEntity"/>
        <set-pk-fields map="parameters" value-field="newEntity"/>
        <set-nonpk-fields map="parameters" value-field="newEntity"/>

        <if-empty field="newEntity.fromDate">
            <now-timestamp field="nowTimestamp"/>
            <set field="newEntity.fromDate" from-field="nowTimestamp"/>
        </if-empty>

        <create-value value-field="newEntity"/>

        <set-service-fields service-name="updateContent" map="parameters" to-map="updateContent"/>
        <call-service service-name="updateContent" in-map-name="updateContent"/>

        <field-to-result field="newEntity.contentId" result-name="contentId"/>
        <field-to-result field="newEntity.productId" result-name="productId"/>
        <field-to-result field="newEntity.productContentTypeId" result-name="productContentTypeId"/>
    </simple-method>
    <simple-method method-name="updateProductContent" short-description="Update Content For Product">
        <make-value entity-name="ProductContent" value-field="lookupPKMap"/>
        <set-pk-fields map="parameters" value-field="lookupPKMap"/>
        <find-by-primary-key map="lookupPKMap" value-field="lookedUpValue"/>
        <set-nonpk-fields map="parameters" value-field="lookedUpValue"/>
        <store-value value-field="lookedUpValue"/>

        <set-service-fields service-name="updateContent" map="parameters" to-map="updateContent"/>
        <call-service service-name="updateContent" in-map-name="updateContent"/>
    </simple-method>
    <simple-method method-name="removeProductContent" short-description="Remove Content From Product">
        <make-value entity-name="ProductContent" value-field="lookupPKMap"/>
        <set-pk-fields map="parameters" value-field="lookupPKMap"/>
        <find-by-primary-key map="lookupPKMap" value-field="lookedUpValue"/>
        <remove-value value-field="lookedUpValue"/>
    </simple-method>

    <!-- Specialized -->
    <simple-method method-name="createEmailContentForProduct" short-description="Create Email Content For Product">
        <set-service-fields service-name="createProductContent" map="parameters" to-map="createProductContent"/>
        <set-service-fields service-name="createEmailContent" map="parameters" to-map="createEmail"/>
        <call-service service-name="createEmailContent" in-map-name="createEmail" break-on-error="true" >
            <result-to-field result-name="contentId" field="createProductContent.contentId"/>
        </call-service>
        <call-service service-name="createProductContent" in-map-name="createProductContent"/>
    </simple-method>
    <simple-method method-name="updateEmailContentForProduct" short-description="Update Email Content For Product">
        <set-service-fields service-name="updateProductContent" map="parameters" to-map="updateProductContent"/>
        <call-service service-name="updateProductContent" in-map-name="updateProductContent"/>
        <set-service-fields service-name="updateEmailContent" map="parameters" to-map="updateEmail"/>
        <call-service service-name="updateEmailContent" in-map-name="updateEmail"/>
    </simple-method>

    <simple-method method-name="createDownloadContentForProduct" short-description="Create Download Content For Product">
        <set-service-fields service-name="createProductContent" map="parameters" to-map="createProductContent"/>
        <set-service-fields service-name="persistContentAndAssoc" map="parameters" to-map="persistContentAndAssoc"/>
        <set field="persistContentAndAssoc.contentTypeId" value="DOCUMENT"/>
        <set field="persistContentAndAssoc.dataResourceTypeId" value="IMAGE_OBJECT"/>
        <set field="persistContentAndAssoc.contentName" from-field="parameters._imageData_fileName"/>
        <set field="persistContentAndAssoc.mimeTypeId" from-field="parameters._imageData_contentType"/>
        <call-service service-name="persistContentAndAssoc" in-map-name="persistContentAndAssoc" break-on-error="true" >
            <result-to-field result-name="contentId" field="createProductContent.contentId"/>
        </call-service>
        <call-service service-name="createProductContent" in-map-name="createProductContent"/>
    </simple-method>
    <simple-method method-name="updateDownloadContentForProduct" short-description="Update Email Content For Product">
        <set-service-fields service-name="updateProductContent" map="parameters" to-map="updateProductContent"/>
        <call-service service-name="updateProductContent" in-map-name="updateProductContent"/>
        <set-service-fields service-name="updateDownloadContent" map="parameters" to-map="updateDownload"/>
        <call-service service-name="updateDownloadContent" in-map-name="updateDownload"/>
    </simple-method>

    <simple-method method-name="createSimpleTextContentForProduct" short-description="Create Simple Text Content For Product">
        <set-service-fields service-name="createProductContent" map="parameters" to-map="createProductContentMap"/>
        <set-service-fields service-name="createSimpleTextContent" map="parameters" to-map="createSimpleTextMap"/>
        <call-service service-name="createSimpleTextContent" in-map-name="createSimpleTextMap">
            <result-to-field result-name="contentId" field="createProductContentMap.contentId"/>
        </call-service>
        <call-service service-name="createProductContent" in-map-name="createProductContentMap"/>
    </simple-method>
    <simple-method method-name="updateSimpleTextContentForProduct" short-description="Update Simple Text Content For Product">
        <set-service-fields service-name="updateProductContent" map="parameters" to-map="updateProductContent"/>
        <call-service service-name="updateProductContent" in-map-name="updateProductContent"/>
        <set-service-fields service-name="updateSimpleTextContent" map="parameters" to-map="updateSimpleText"/>
        <call-service service-name="updateSimpleTextContent" in-map-name="updateSimpleText"/>
    </simple-method>
    <simple-method method-name="createSimpleTextContentForAlternateLocale" short-description="Create Simple Text Content For Alternate Locale">
        <set-service-fields service-name="createSimpleTextContent" map="parameters" to-map="createSimpleTextMap"/>
        <call-service service-name="createSimpleTextContent" in-map-name="createSimpleTextMap">
            <result-to-field result-name="contentId" field="createContentAssocMap.contentIdTo"/>
        </call-service>
        <set field="createContentAssocMap.contentId" from-field="parameters.mainContentId"/>
        <set field="createContentAssocMap.contentAssocTypeId" value="ALTERNATE_LOCALE"/>
        <call-service service-name="createContentAssoc" in-map-name="createContentAssocMap"/>
    </simple-method>

    <simple-method method-name="uploadProductAdditionalViewImages" short-description="Method to upload multiple Additional View images for product">
        <set-service-fields service-name="addAdditionalViewForProduct" map="parameters" to-map="addAdditionalViewForProductMap"/>
        <if-not-empty field="parameters.additionalImageOne">
            <set field="addAdditionalViewForProductMap.productId" from-field="parameters.productId"/>
            <set field="addAdditionalViewForProductMap.uploadedFile" from-field="parameters.additionalImageOne"/>
            <set field="addAdditionalViewForProductMap.productContentTypeId" value="ADDITIONAL_IMAGE_1"/>
            <set field="addAdditionalViewForProductMap._uploadedFile_fileName" from-field="parameters._additionalImageOne_fileName"/>
            <set field="addAdditionalViewForProductMap._uploadedFile_contentType" from-field="parameters._additionalImageOne_contentType"/>
            <call-service service-name="addAdditionalViewForProduct" in-map-name="addAdditionalViewForProductMap"/>
            <clear-field field="addAdditionalViewForProductMap"/>
        </if-not-empty>
        <if-not-empty field="parameters.additionalImageTwo">
            <set field="addAdditionalViewForProductMap.productId" from-field="parameters.productId"/>
            <set field="addAdditionalViewForProductMap.uploadedFile" from-field="parameters.additionalImageTwo"/>
            <set field="addAdditionalViewForProductMap.productContentTypeId" value="ADDITIONAL_IMAGE_2"/>
            <set field="addAdditionalViewForProductMap._uploadedFile_fileName" from-field="parameters._additionalImageTwo_fileName"/>
            <set field="addAdditionalViewForProductMap._uploadedFile_contentType" from-field="parameters._additionalImageTwo_contentType"/>
            <call-service service-name="addAdditionalViewForProduct" in-map-name="addAdditionalViewForProductMap"/>
            <clear-field field="addAdditionalViewForProductMap"/>
        </if-not-empty>
        <if-not-empty field="parameters.additionalImageThree">
            <set field="addAdditionalViewForProductMap.productId" from-field="parameters.productId"/>
            <set field="addAdditionalViewForProductMap.uploadedFile" from-field="parameters.additionalImageThree"/>
            <set field="addAdditionalViewForProductMap.productContentTypeId" value="ADDITIONAL_IMAGE_3"/>
            <set field="addAdditionalViewForProductMap._uploadedFile_fileName" from-field="parameters._additionalImageThree_fileName"/>
            <set field="addAdditionalViewForProductMap._uploadedFile_contentType" from-field="parameters._additionalImageThree_contentType"/>
            <call-service service-name="addAdditionalViewForProduct" in-map-name="addAdditionalViewForProductMap"/>
            <clear-field field="addAdditionalViewForProductMap"/>
        </if-not-empty>
        <if-not-empty field="parameters.additionalImageFour">
            <set field="addAdditionalViewForProductMap.productId" from-field="parameters.productId"/>
            <set field="addAdditionalViewForProductMap.uploadedFile" from-field="parameters.additionalImageFour"/>
            <set field="addAdditionalViewForProductMap.productContentTypeId" value="ADDITIONAL_IMAGE_4"/>
            <set field="addAdditionalViewForProductMap._uploadedFile_fileName" from-field="parameters._additionalImageFour_fileName"/>
            <set field="addAdditionalViewForProductMap._uploadedFile_contentType" from-field="parameters._additionalImageFour_contentType"/>
            <call-service service-name="addAdditionalViewForProduct" in-map-name="addAdditionalViewForProductMap"/>
            <clear-field field="addAdditionalViewForProductMap"/>
        </if-not-empty>
        <field-to-result field="parameters.productId" result-name="productId"/>
    </simple-method>

    <!-- ProductPromoContent -->
    <simple-method method-name="removeProductPromoContent" short-description="Remove Content From Product Promo">
        <set-service-fields service-name="updateProductPromoContent" map="parameters" to-map="updateProductPromoContentMap"/>
        <if-empty field="updateProductPromoContentMap.thruDate">
            <now-timestamp field="updateProductPromoContentMap.thruDate"/>
        </if-empty>
        <call-service service-name="updateProductPromoContent" in-map-name="updateProductPromoContentMap"/>
    </simple-method>

    <!-- Product Content SEO -->
    <simple-method method-name="updateContentSEOForProduct" short-description="Update Product SEO">
        <if-not-empty field="parameters.title">
            <entity-and entity-name="ProductContentAndInfo" list="productContents">
                <field-map field-name="productId" from-field="parameters.productId"/>
                <field-map field-name="productContentTypeId" value="PAGE_TITLE"/>
            </entity-and>
            <if-not-empty field="productContents">
                <first-from-list entry="productContent" list="productContents"/>
                <entity-one entity-name="ElectronicText" value-field="electronicText">
                    <field-map field-name="dataResourceId" from-field="productContent.dataResourceId"/>
                </entity-one>
                <if-not-empty field="electronicText">
                    <set field="electronicText.textData" from-field="parameters.title"/>
                    <store-value value-field="electronicText"/>
                </if-not-empty>
            <else>
                <set field="createTextContentMap.productId" from-field="parameters.productId"/>
                <set field="createTextContentMap.productContentTypeId" value="PAGE_TITLE"/>
                <set field="createTextContentMap.text" from-field="parameters.title"/>
                <call-service service-name="createSimpleTextContentForProduct" in-map-name="createTextContentMap"/>
            </else>
            </if-not-empty>
        </if-not-empty>
        <if-not-empty field="parameters.metaKeyword">
            <entity-and entity-name="ProductContentAndInfo" list="productContents">
                <field-map field-name="productId" from-field="parameters.productId"/>
                <field-map field-name="productContentTypeId" value="META_KEYWORD"/>
            </entity-and>
            <if-not-empty field="productContents">
                <first-from-list entry="productContent" list="productContents"/>
                <entity-one entity-name="ElectronicText" value-field="electronicText">
                    <field-map field-name="dataResourceId" from-field="productContent.dataResourceId"/>
                </entity-one>
                <if-not-empty field="electronicText">
                    <set field="electronicText.textData" from-field="parameters.metaKeyword"/>
                    <store-value value-field="electronicText"/>
                </if-not-empty>
            <else>
                <set field="createTextContentMap.productId" from-field="parameters.productId"/>
                <set field="createTextContentMap.productContentTypeId" value="META_KEYWORD"/>
                <set field="createTextContentMap.text" from-field="parameters.metaKeyword"/>
                <call-service service-name="createSimpleTextContentForProduct" in-map-name="createTextContentMap"/>
            </else>
            </if-not-empty>
        </if-not-empty>
        <if-not-empty field="parameters.metaDescription">
            <entity-and entity-name="ProductContentAndInfo" list="productContents">
                <field-map field-name="productId" from-field="parameters.productId"/>
                <field-map field-name="productContentTypeId" value="META_DESCRIPTION"/>
            </entity-and>
            <if-not-empty field="productContents">
                <first-from-list entry="productContent" list="productContents"/>
                <entity-one entity-name="ElectronicText" value-field="electronicText">
                    <field-map field-name="dataResourceId" from-field="productContent.dataResourceId"/>
                </entity-one>
                <if-not-empty field="electronicText">
                    <set field="electronicText.textData" from-field="parameters.metaDescription"/>
                    <store-value value-field="electronicText"/>
                </if-not-empty>
            <else>
                <set field="createTextContentMap.productId" from-field="parameters.productId"/>
                <set field="createTextContentMap.productContentTypeId" value="META_DESCRIPTION"/>
                <set field="createTextContentMap.text" from-field="parameters.metaDescription"/>
                <call-service service-name="createSimpleTextContentForProduct" in-map-name="createTextContentMap"/>
            </else>
            </if-not-empty>
        </if-not-empty>
    </simple-method>
</simple-methods>
