<?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">
    <simple-method method-name="createCustRequestContent"
        short-description="Create Customer Request Content" login-required="false">
        <call-class-method class-name="org.apache.ofbiz.content.layout.LayoutWorker"
            method-name="uploadImageAndParameters" ret-field="formInput">
            <field field="request" type="javax.servlet.http.HttpServletRequest"/>
            <string value="dataResourceName"/>
        </call-class-method>

        <!-- Check input parameters -->
        <if>
            <condition>
                <and>
                    <if-empty field="formInput.formInput.contentId"/>
                    <if-empty field="formInput.imageFileName"/>
                </and>
            </condition>
            <then>
                <add-error>
                    <fail-property resource="ContentUiLabels" property="ContentContentIdOrUploadFileIsMissing"/>
                </add-error>
            </then>
        </if>
        <check-errors/>
        <!-- Create new content form upload file / set content from Existing content -->
        <if-empty field="formInput.formInput.contentId">
                <if>
                    <condition>
                        <or>
                            <!-- Compaire MimeType between upload file and selected MimeType -->
                            <if-compare-field field="formInput.uploadMimeType" operator="equals" to-field="formInput.formInput.mimeTypeId"/>
                            <if-compare field="formInput.formInput.mimeTypeId" operator="equals"  value="application/octet-stream"/>
                            <if-compare field="formInput.formInput.mimeTypeId" operator="equals"  value=""/>
                        </or>
                    </condition>
                    <then>
                        <!-- Create new content from upload file -->
                        <set-service-fields service-name="createContentFromUploadedFile"
                            map="formInput.formInput" to-map="inMap"/>
                        <set field="inMap._uploadedFile_fileName" from-field="formInput.imageFileName"/>
                        <set field="inMap.uploadedFile" from-field="formInput.imageData"/>
                        <set field="inMap._uploadedFile_contentType" from-field="formInput.uploadMimeType"/>
                        <call-service service-name="createContentFromUploadedFile" in-map-name="inMap">
                            <result-to-field result-name="contentId" field="context.contentId"/>
                        </call-service>
                    </then>
                    <else>
                        <add-error>
                            <fail-property resource="ContentUiLabels" property="ContentUploadFileTypeNotMatch"/>
                        </add-error>
                    </else>
                </if>
                <check-errors/>
            <else>
                <!--set content from Existing content-->
                <set field="context.contentId" from-field="formInput.formInput.contentId"/>
            </else>
        </if-empty>
        
        <!-- create ContentAssoc-->
        <set-service-fields service-name="createContentAssoc" map="formInput.formInput" to-map="contentMap"/>
        <if-not-empty field="formInput.formInput.contentIdFrom">
            <set field="contentMap.contentAssocTypeId" value="SUB_CONTENT"/>
            <set field="contentMap.contentIdFrom" from-field="formInput.formInput.contentIdFrom"/>
            <set field="contentMap.contentId" from-field="formInput.formInput.contentIdFrom"/>
            <set field="contentMap.contentIdTo" from-field="context.contentId"/>
            <now-timestamp field="contentMap.fromDate"/>
            <entity-and entity-name="ContentAssoc" list="contentAssoList">
                <field-map field-name="contentId" from-field="contentMap.contentId"/>
                <field-map field-name="contentIdTo" from-field="contentMap.contentIdTo"/>
            </entity-and>
            <if-empty field="contentAssonList">
                <call-service service-name="createContentAssoc" in-map-name="contentMap"/>           
            </if-empty>
        </if-not-empty>
        
        <set field="context.custRequestId" from-field="formInput.formInput.custRequestId"/>
        <call-map-processor in-map-name="context" out-map-name="custRequestContext">
            <simple-map-processor name="newCustRequestContent">
                <process field="contentId">
                    <copy to-field="contentId"/>
                </process>
                <process field="custRequestId">
                    <copy to-field="custRequestId"/>
                </process>
                <process field="fromDate">
                    <copy to-field="fromDate"/>
                </process>
            </simple-map-processor>
        </call-map-processor>

        <!-- Create CustRequestContent -->
        <call-service service-name="createCustRequestContent" in-map-name="custRequestContext">
            <result-to-field result-name="contentId" field="contentId"/>
        </call-service>
    </simple-method>
</simple-methods>
