<?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="createCommunicationEventContent"
        short-description="Upload Content and Create Communication Content Association">
        <!-- Call layout to get field -->
        <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="uploadedFile"/>
        </call-class-method>

        <if-empty field="formInput.formInput.contentId">
            <if-empty field="formInput.imageFileName">
                <add-error>
                    <fail-property resource="PartyUiLabels" property="PartyCreateCommunicationEventUploadFileMissing"/>
                </add-error>
            </if-empty>
            <check-errors/>
            
            <set field="parameters.imageFileName" from-field="formInput.imageFileName"/>
            
            <!-- Create Data Resource -->
            <set-service-fields service-name="createContentFromUploadedFile"
                map="formInput.formInput" to-map="data"/>
            <set field="data.dataResourceTypeId" value="LOCAL_FILE"/>
            <set field="data.dataTemplateTypeId" value="NONE"/>
            <set field="data.dataCategoryId" from-field="formInput.formInput.dataCategoryId"/>
            
            <set field="data.statusId" from-field="formInput.formInput.resourceStatusId"/>
            <set field="data.dataResourceName" from-field="formInput.imageFileName"/>
            <set field="data.mimeTypeId" from-field="mimeType.mimeTypeId"/>
            <set field="data.uploadedFile" from-field="formInput.imageData"/>
            <set field="data._uploadedFile_fileName" from-field="formInput.imageFileName"/>
            <set field="data._uploadedFile_contentType" from-field="formInput.formInput.mimeTypeId"/>
            <call-service service-name="createDataResource" in-map-name="data">
                <result-to-field result-name="dataResourceId" field="parameters.dataResourceId"/>
            </call-service>
            
            <!-- Create attach upload to data resource -->
            <set-service-fields service-name="attachUploadToDataResource" map="formInput.formInput"
                to-map="attachMap"/>
            <set field="attachMap.uploadedFile" from-field="formInput.imageData"/>
            <set field="attachMap._uploadedFile_fileName" from-field="formInput.imageFileName"/>
            <set field="attachMap._uploadedFile_contentType" from-field="formInput.formInput.mimeTypeId"/>
            <set field="attachMap.dataResourceId" from-field="parameters.dataResourceId"/>
            <set field="attachMap.mimeTypeId" from-field="mimeType.mimeTypeId"/>
            <call-service service-name="attachUploadToDataResource" in-map-name="attachMap"/>
            
            <!-- Create content from dataResource -->
            <set-service-fields service-name="createContentFromDataResource"
                map="formInput.formInput" to-map="contentMap"/>
            <set field="contentMap.roleTypeId" from-field="formInput.formInput.roleTypeId"/>
            <set field="contentMap.partyId" from-field="formInput.formInput.partyId"/>
            <set field="contentMap.contentTypeId" from-field="formInput.formInput.contentTypeId"/>
            <set field="contentMap.dataResourceId" from-field="parameters.dataResourceId"/>
            <call-service service-name="createContentFromDataResource" in-map-name="contentMap">
                <result-to-field result-name="contentId" field="parameters.contentId"/>
            </call-service>
            <log level="info" message=" Content : ${parameters.contentId}"/>
        </if-empty>
        <!-- Create party content -->
        <set-service-fields service-name="createPartyContent" map="formInput.formInput"
            to-map="partycontent"/>
        <set field="partycontent.contentId" from-field="parameters.contentId"/>
        <set field="partycontent.partyContentTypeId"
            from-field="formInput.formInput.partyContentTypeId"/>
        <set field="partycontent.partyId" from-field="formInput.formInput.partyId"/>
        <call-service service-name="createPartyContent" in-map-name="partycontent"/>

        <!-- save header fields -->
        <set-service-fields service-name="updateCommunicationEvent" map="formInput.formInput" to-map="updateMap"/>
        <call-service service-name="updateCommunicationEvent" in-map-name="updateMap"/>

        <!-- Create communication event and content association -->
        <set-service-fields service-name="createCommEventContentAssoc"
            map="formInput.formInput" to-map="contentAssoc"/>
        <set field="contentAssoc.contentId" from-field="parameters.contentId"/>
        <set field="contentAssoc.communicationEventId"
            from-field="formInput.formInput.communicationEventId"/>
        <call-service service-name="createCommEventContentAssoc" in-map-name="contentAssoc">
            <result-to-field result-name="fromDate" field="fromDate"/>
        </call-service>

        <!-- 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="parameters.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>

        <!-- Return to request -->
        <field-to-request field="formInput.formInput.communicationEventId" request-name="communicationEventId"/>
        <set field="my" value="My"/>
        <field-to-request field="my"/>
    </simple-method>
    <simple-method method-name="allocateMsgToParty" short-description="Allocate an emailaddress to an existing/new party, update the communication event accordingly">
        <entity-one entity-name="CommunicationEvent" value-field="communicationEvent"/>
        <if-empty field="communicationEvent">
            <add-error>
                <fail-property resource="PartyUiLabels" property="PartyCommunicationEventNotFound"/>
            </add-error>
        </if-empty>
        
        <if-empty field="parameters.partyId">
            <if-empty field="parameters.emailAddress">
                <add-error>
                    <fail-property resource="PartyUiLabels" property="PartyEmailAddressRequired"/>
                </add-error>
            </if-empty>
            
            <if-empty field="parameters.lastName">
                <add-error>
                    <fail-property resource="PartyUiLabels" property="PartyLastNameRequested"/>
                </add-error>
            </if-empty>
            
            <if-empty field="parameters.firstName">
                <add-error>
                    <fail-property resource="PartyUiLabels" property="PartyFirstNameRequested"/>
                </add-error>
                <check-errors/>
            </if-empty>
        </if-empty>
        
        <check-errors/>
        
        <!-- create partyId if not provided -->
        <if-empty field="parameters.partyId">
            <set-service-fields service-name="createPerson" map="parameters" to-map="newParty"/>
            <call-service service-name="createPerson" in-map-name="newParty">
                <result-to-field result-name="partyId" field="parameters.partyId"/>
            </call-service>
        </if-empty>
        
        <!-- allocate email to the party -->
        <if-not-empty field="parameters.emailAddress">
            <entity-one entity-name="Party" value-field="party"/>
            <if-empty field="party">
                <add-error>
                    <fail-property resource="PartyUiLabels" property="PartyPartyIdMissing"/>
                </add-error>
                <check-errors />
            </if-empty>
            <set field="newEmail.contactMechPurposeTypeId" value="PRIMARY_EMAIL"/>
            <set field="newEmail.partyId" from-field="parameters.partyId"/>
            <set field="newEmail.emailAddress" from-field="parameters.emailAddress"/>
            <call-service service-name="createPartyEmailAddress"
                in-map-name="newEmail">
                <result-to-field result-name="contactMechId" field="inCom.contactMechIdFrom"/>
            </call-service>
        </if-not-empty>
        
        <!-- update the communication event -->
        <set field="inCom.communicationEventId" from-field="parameters.communicationEventId"/>
        <set field="inCom.partyIdFrom" from-field="parameters.partyId"/>
        <set field="inCom.statusId" value="COM_ENTERED"/>
        <call-service service-name="updateCommunicationEvent" in-map-name="inCom"/>
        
        <field-to-request field="parameters.communicationEventId" request-name="communicationEventId"/>
        <entity-one entity-name="PartyNameView" value-field="nameView"/>
        <set field="_event_message_" value="Email addres: ${parameters.emailAddress} allocated to party: ${nameView.groupName}${nameView.firstName} ${nameView.middleName} ${nameView.lastName}[${parameters.partyId}]"/>
    </simple-method>
    
</simple-methods>
