<?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="movePortletToPortalPage" short-description="Moves a PortalPortlet from the actual portalPage to a different one">
        <call-simple-method method-name="checkOwnerShip"/>
        <entity-one entity-name="PortalPagePortlet" value-field="oldEntity"/>
        <set field="parameters.portalPageId" from-field="parameters.newPortalPageId"/>
        <call-simple-method method-name="copyIfRequiredSystemPage" xml-resource="component://common/minilang/PortalPageMethods.xml"/>
        <set field="parameters.newPortalPageId" from-field="portalPageId"/>
        <make-value entity-name="PortalPagePortlet" value-field="newEntity"/>
        <set field="newEntity.portalPortletId" from-field="parameters.portalPortletId"/>
        <set field="newEntity.portalPageId" from-field="parameters.newPortalPageId"/>
        <set field="newEntity.columnNum" value="1"/>
        <make-next-seq-id value-field="newEntity" seq-field-name="portletSeqId"/>
        <create-value value-field="newEntity"/>
        <remove-value value-field="oldEntity"/>
    </simple-method>

    <simple-method method-name="addPortalPageColumn" short-description="Add a new Column to a PortalPage">
        <call-simple-method method-name="checkOwnerShip"/>
        <make-value entity-name="PortalPageColumn" value-field="newEntity"/>
        <set-pk-fields map="parameters" value-field="newEntity"/>
        <if-empty field="parameters.columnSeqId">
            <make-next-seq-id value-field="newEntity" seq-field-name="columnSeqId"/>
        </if-empty>
        <field-to-result field="newEntity.columnSeqId" result-name="columnSeqId"/>
        <set-nonpk-fields map="parameters" value-field="newEntity"/>
        <create-value value-field="newEntity"/>
    </simple-method>

    <simple-method method-name="deletePortalPageColumn" short-description="Delete a Column from a PortalPage">
        <call-simple-method method-name="checkOwnerShip"/>
        <entity-one entity-name="PortalPageColumn" value-field="column" auto-field-map="true"/>
        <if-not-empty field="column">
            <entity-and entity-name="PortalPagePortlet" list="portalPortletList">
                <field-map field-name="portalPageId" from-field="column.portalPageId"/>
                <field-map field-name="columnSeqId"  from-field="column.columnSeqId"/>
            </entity-and>
            <iterate list="portalPortletList" entry="portalPortlet">
                <set-service-fields service-name="deletePortalPagePortlet" map="portalPortlet" to-map="deletePortalPagePortletInMap"/>
                <call-service service-name="deletePortalPagePortlet" in-map-name="deletePortalPagePortletInMap"/>
            </iterate>
            <remove-value value-field="column"/>
        </if-not-empty>
        <check-errors/>
    </simple-method>

    <simple-method method-name="createPortalPagePortlet" short-description="Add a registered PortalPortlet to a PortalPage">
        <call-simple-method method-name="checkOwnerShip"/>
        <make-value entity-name="PortalPagePortlet" value-field="newEntity"/>
        <set-pk-fields map="parameters" value-field="newEntity"/>

        <entity-condition entity-name="PortalPagePortlet" list="portlets">
            <condition-expr field-name="portalPageId" operator="equals" from-field="parameters.portalPageId"/>
            <select-field field-name="sequenceNum"/>
            <order-by field-name="-sequenceNum"/>
            <limit-range size="1" start="0"/>
        </entity-condition>
        <first-from-list list="portlets" entry="portalPagePortlet"/>

        <if-empty field="portalPagePortlet.sequenceNum">
            <calculate field="newEntity.sequenceNum" type="Long"><number value="1"/></calculate>
            <else>
                <calculate field="newEntity.sequenceNum" type="Long">
                    <calcop operator="add" field="portalPagePortlet.sequenceNum">
                        <number value="1"/>
                    </calcop>
                </calculate>
            </else>
        </if-empty>

        <make-next-seq-id value-field="newEntity" seq-field-name="portletSeqId"/>
        <field-to-result field="newEntity.portletSeqId" result-name="portletSeqId"/>
        <set-nonpk-fields map="parameters" value-field="newEntity"/>
        <create-value value-field="newEntity"/>
    </simple-method>

    <simple-method method-name="deletePortalPagePortlet" short-description="Delete a PortalPortlet from a PortalPageColumn">
        <call-simple-method method-name="checkOwnerShip"/>
        <entity-one entity-name="PortalPagePortlet" value-field="portlet" auto-field-map="true"/>
        <if-not-empty field="portlet">
            <make-value entity-name="PortletAttribute" value-field="newEntity"/>
            <set field="newEntity.portalPageId" from-field="portlet.portalPageId"/>
            <set field="newEntity.portalPortletId" from-field="portlet.portalPortletId"/>
            <set field="newEntity.portletSeqId" from-field="portlet.portletSeqId"/>
            <remove-by-and entity-name="PortletAttribute" map="newEntity"/>
            <remove-value value-field="portlet"/>
        </if-not-empty>
    </simple-method>

    <simple-method method-name="getPortletAttributes" short-description="Get all attributes of a Portlet either by providing userLogin or portalPageid with portalPortletId" login-required="true">
        <if-empty field="parameters.ownerUserLoginId">
            <if-empty field="parameters.portalPageId">
                <log level="error" message="Service getPortletAttributes did not receive either ownerUserLoginId OR portalPageId"></log>
                <string-to-list string="Service getPortletAttributes did not receive either ownerUserLoginId OR portalPageId" list="error_list"/>
            </if-empty>
        </if-empty>
        <check-errors/>
        <!-- if userLogin provided get the portalPageId with the required portlet -->
        <if-not-empty field="parameters.ownerUserLoginId">
            <entity-and entity-name="PortalPageAndPortlet" list="ppList">
                <field-map field-name="ownerUserLoginId" from-field="parameters.ownerUserLoginId"/>
                <field-map field-name="portalPortletId"  from-field="parameters.portalPortletId"/>
            </entity-and>
            <first-from-list list="ppList" entry="portalPage"/>
            <set field="parameters.portalPageId" from-field="portalPage.portalPageId"/>
        </if-not-empty>
        <entity-condition entity-name="PortletAttribute" list="attributeList">
            <condition-list combine="and">
                <condition-expr field-name="portalPageId" operator="equals" from-field="parameters.portalPageId"/>
                <condition-expr field-name="portalPortletId" operator="equals" from-field="parameters.portalPortletId"/>
                <condition-expr field-name="portletSeqId" operator="equals" from-field="parameters.portletSeqId" ignore-if-empty="true"/>
            </condition-list>
        </entity-condition>
        <if-not-empty field="attributeList">
            <iterate list="attributeList" entry="attributeRecord">
                <set field="attributeMap.${attributeRecord.attrName}" from-field="attributeRecord.attrValue"/>
            </iterate>
            <field-to-result field="attributeMap" result-name="attributeMap"/>
        </if-not-empty>
    </simple-method>

    <simple-method method-name="createPortalPage" short-description="Create a new Portal Page">
        <if-not-empty field="parameters.portalPageName">
            <make-value entity-name="PortalPage" value-field="newPortalPage"/>
            <set-pk-fields map="parameters" value-field="newPortalPage"/>
            <if-empty field="newPortalPage.portalPageId">
                <sequenced-id sequence-name="PortalPage" field="newPortalPage.portalPageId"/>
            </if-empty>
            <set-nonpk-fields map="parameters" value-field="newPortalPage"/>
            <set field="newPortalPage.ownerUserLoginId" from-field="parameters.userLogin.userLoginId"/>
            <if-empty field="parameters.sequenceNum">
                <make-next-seq-id value-field="newPortalPage" seq-field-name="sequenceNum"/>
            </if-empty>
            <create-value value-field="newPortalPage"/>
            <field-to-result field="newPortalPage.portalPageId" result-name="portalPageId"/>
        </if-not-empty>
    </simple-method>

    <simple-method method-name="deletePortalPage" short-description="Delete a Portal Page">
        <entity-one entity-name="PortalPage" value-field="getPortalPage"/>
        <if-not-empty field="getPortalPage.originalPortalPageId">
            <entity-one entity-name="PortalPage" value-field="getOldSequenceNum">
                <field-map field-name="portalPageId" from-field="getPortalPage.originalPortalPageId"/>
            </entity-one>
            <entity-condition entity-name="PortalPage" list="checkSequenceNums">
                <condition-list combine="and">
                    <condition-expr field-name="sequenceNum" operator="equals" from-field="getOldSequenceNum.sequenceNum"/>
                    <condition-expr field-name="ownerUserLoginId" operator="equals" from-field="userLogin.userLoginId"/>
                    <condition-expr field-name="parentPortalPageId" operator="equals" from-field="parameters.parentPortalPageId"/>
                </condition-list>
            </entity-condition>
            <first-from-list list="checkSequenceNums" entry="checkSequenceNum"/>
            <if-not-empty field="checkSequenceNum.portalPageId">
                <set field="first.portalPageId" from-field="checkSequenceNum.portalPageId"/>
                <set field="first.sequenceNum" from-field="getPortalPage.sequenceNum" type="Long"/>
                <call-service service-name="updatePortalPage" in-map-name="first"/>
            </if-not-empty>
        </if-not-empty>
        <call-simple-method method-name="checkOwnerShip"/>
        <remove-related value-field="portalPage" relation-name="PortalPageColumn"/>
        <remove-related value-field="portalPage" relation-name="PortalPagePortlet"/>
        <remove-value value-field="portalPage"/>
    </simple-method>

    <simple-method method-name="checkOwnerShip" short-description="Check the ownership of a Portal Page">
        <if-not-empty field="parameters.portalPageId">
            <entity-one entity-name="PortalPage" value-field="portalPage"/>
            <if-empty field="portalPage">
                <add-error>
                    <fail-property resource="CommonUiLabels" property="PortalPageNotFound"/>
                </add-error>
            </if-empty>
            <check-errors/>
            <if> <!-- only page owner or user with MYPORTALBASE_ADMIN can modify the page detail -->
              <condition>
                <and>
                  <if-compare field="portalPage.ownerUserLoginId" operator="not-equals" value="${parameters.userLogin.userLoginId}"/>
                  <not>
                       <if-has-permission permission="MYPORTALBASE" action="_ADMIN"/>
                  </not>
                </and>
              </condition>
              <then>
                <add-error>
                    <fail-property resource="CommonUiLabels" property="PortalPageNotOwned"/>
                </add-error>
              </then>
            </if>
            <check-errors/>
        </if-not-empty>
    </simple-method>
    <simple-method method-name="updatePortalPageSeq" short-description="Update the portal page sequence numbers">
        <call-simple-method method-name="checkOwnerShip"/>
        <entity-one entity-name="PortalPage" value-field="getSequenceNum"/>
        <if-compare field="parameters.mode" value="UP" operator="equals">
            <entity-condition entity-name="PortalPage" list="getDatas">
                <condition-list combine="and">
                    <condition-expr field-name="sequenceNum" operator="less" from-field="getSequenceNum.sequenceNum"/>
                    <condition-expr field-name="ownerUserLoginId" operator="equals" from-field="userLogin.userLoginId"/>
                    <condition-list combine="or">
                        <condition-expr field-name="parentPortalPageId" operator="equals" from-field="parameters.parentPortalPageId"/>
                        <condition-expr field-name="originalPortalPageId" operator="equals" from-field="parameters.parentPortalPageId"/>
                    </condition-list>
                </condition-list>
                <order-by field-name="-sequenceNum"/>
            </entity-condition>
        </if-compare>
        <if-compare field="parameters.mode" value="DWN" operator="equals">
            <entity-condition entity-name="PortalPage" list="getDatas">
                <condition-list combine="and">
                    <condition-expr field-name="sequenceNum" operator="greater" from-field="getSequenceNum.sequenceNum"/>
                    <condition-expr field-name="ownerUserLoginId" operator="equals" from-field="userLogin.userLoginId"/>
                    <condition-list combine="or">
                        <condition-expr field-name="parentPortalPageId" operator="equals" from-field="parameters.parentPortalPageId"/>
                        <condition-expr field-name="originalPortalPageId" operator="equals" from-field="parameters.parentPortalPageId"/>
                    </condition-list>
                </condition-list>
                <order-by field-name="sequenceNum"/>
            </entity-condition>
        </if-compare>
        <if-compare field="parameters.mode" value="TOP" operator="equals">
            <entity-condition entity-name="PortalPage" list="getDatas">
                <condition-list combine="and">
                    <condition-expr field-name="sequenceNum" operator="less" from-field="getSequenceNum.sequenceNum"/>
                    <condition-expr field-name="ownerUserLoginId" operator="equals" from-field="userLogin.userLoginId"/>
                    <condition-list combine="or">
                        <condition-expr field-name="parentPortalPageId" operator="equals" from-field="parameters.parentPortalPageId"/>
                        <condition-expr field-name="originalPortalPageId" operator="equals" from-field="parameters.parentPortalPageId"/>
                    </condition-list>
                </condition-list>
                <order-by field-name="sequenceNum"/>
            </entity-condition>
        </if-compare>
        <if-compare field="parameters.mode" value="BOT" operator="equals">
            <entity-condition entity-name="PortalPage" list="getDatas">
                <condition-list combine="and">
                    <condition-expr field-name="sequenceNum" operator="greater" from-field="getSequenceNum.sequenceNum"/>
                    <condition-expr field-name="ownerUserLoginId" operator="equals" from-field="userLogin.userLoginId"/>
                    <condition-list combine="or">
                        <condition-expr field-name="parentPortalPageId" operator="equals" from-field="parameters.parentPortalPageId"/>
                        <condition-expr field-name="originalPortalPageId" operator="equals" from-field="parameters.parentPortalPageId"/>
                    </condition-list>
                </condition-list>
                <order-by field-name="-sequenceNum"/>
            </entity-condition>
        </if-compare>
        <first-from-list list="getDatas" entry="getData"/>
        <set field="portalPage.sequenceNum" from-field="getData.sequenceNum" type="Long"/>
        <store-value value-field="portalPage"/>
        <set field="first.portalPageId" from-field="getData.portalPageId"/>
        <set field="first.sequenceNum" from-field="getSequenceNum.sequenceNum" type="Long"/>
        <call-service service-name="updatePortalPage" in-map-name="first"/>
    </simple-method>

    <!-- update Portlet Seq with Drag & Drop -->
    <simple-method method-name="updatePortletSeqDragDrop" short-description="Updates a portlet Seq No for the Drag and Drop Feature">
        <set field="parameters.portalPageId" from-field="parameters.o_portalPageId"/>
        <call-simple-method method-name="checkOwnerShip"/>

        <!-- origin Portlet -->
        <entity-one entity-name="PortalPagePortlet" value-field="originPp">
            <field-map field-name="portalPageId" from-field="parameters.o_portalPageId"/>
            <field-map field-name="portalPortletId" from-field="parameters.o_portalPortletId"/>
            <field-map field-name="portletSeqId" from-field="parameters.o_portletSeqId"/>
        </entity-one>
        <if-empty field="originPp">
            <return response-code="error"/>
        </if-empty>
        <set field="columnSeqId" from-field="parameters.destinationColumn" default-value="${originPp.columnSeqId}"/>

        <!-- destination Portlet -->
        <if-compare field="parameters.mode" operator="contains" value="DRAGDROPBEFORE">
            <entity-one value-field="destiPp" entity-name="PortalPagePortlet">
                <field-map field-name="portalPageId" from-field="parameters.d_portalPageId"/>
                <field-map field-name="portalPortletId" from-field="parameters.d_portalPortletId"/>
                <field-map field-name="portletSeqId" from-field="parameters.d_portletSeqId"/>
            </entity-one>

            <entity-condition entity-name="PortalPagePortlet" list="modifyPpList">
             <condition-list combine="and">
                <condition-expr field-name="portalPageId" operator="equals" from-field="parameters.portalPageId"/>
                <condition-expr field-name="columnSeqId" operator="equals" from-field="columnSeqId"/>
                <condition-expr field-name="sequenceNum" operator="greater-equals" from-field="destiPp.sequenceNum"/>
                <condition-expr field-name="sequenceNum" operator="less" from-field="originPp.sequenceNum" ignore-if-empty="true"/>
             </condition-list>
             <order-by field-name="sequenceNum"/>
            </entity-condition>
            <set field="increase" value="1" type="Long"/>
            <set field="newSequenceNo" from-field="destiPp.sequenceNum"/>
        </if-compare>

        <if-compare field="parameters.mode" operator="equals" value="DRAGDROPAFTER">
            <entity-one value-field="destiPp" entity-name="PortalPagePortlet">
                <field-map field-name="portalPageId" from-field="parameters.d_portalPageId"/>
                <field-map field-name="portalPortletId" from-field="parameters.d_portalPortletId"/>
                <field-map field-name="portletSeqId" from-field="parameters.d_portletSeqId"/>
            </entity-one>

            <entity-condition entity-name="PortalPagePortlet" list="modifyPpList">
             <condition-list combine="and">
                <condition-expr field-name="portalPageId" operator="equals" from-field="parameters.portalPageId"/>
                <condition-expr field-name="columnSeqId" operator="equals" from-field="columnSeqId"/>
                <condition-expr field-name="sequenceNum" operator="greater" from-field="originPp.sequenceNum"/>
                <condition-expr field-name="sequenceNum" operator="less-equals" from-field="destiPp.sequenceNum" ignore-if-empty="true"/>
             </condition-list>
             <order-by field-name="-sequenceNum"/>
            </entity-condition>
            <set field="increase" value="-1" type="Long"/>
            <set field="newSequenceNo" from-field="destiPp.sequenceNum"/>
        </if-compare>

        <if-compare field="parameters.mode" operator="contains" value="NEW">
            <set field="newSequenceNo" value="0"/>
        </if-compare>

        <!-- Modify the sequence id from the following portlets -->
        <if-not-empty field="modifyPpList">
            <iterate list="modifyPpList" entry="modifyPp">
                <if-empty field="modifyPp.sequenceNum">
                    <set field="modifyPp.sequenceNum" value="newSequenceNo"/>
                    <else>
                        <calculate field="modifyPp.sequenceNum" type="Long">
                            <calcop operator="add" field="newSequenceNo">
                                <calcop operator="get" field="increase"/>
                            </calcop>
                        </calculate>
                        <calculate field="increase" type="Long">
                            <calcop operator="add" field="increase">
                                <calcop operator="get" field="increase"/>
                            </calcop>
                        </calculate>
                    </else>
                </if-empty>
                <store-value value-field="modifyPp"/>
            </iterate>
        </if-not-empty>
        <!-- Create a new db entry for the moved portlet -->
        <set field="originPp.columnSeqId" from-field="columnSeqId"/>
        <set field="originPp.sequenceNum" from-field="newSequenceNo" type="Long"/>
        <store-value value-field="originPp"/>
    </simple-method>
    <!--  -->
</simple-methods>
