Package de.innovationgate.wgpublisher.hdb

Source Code of de.innovationgate.wgpublisher.hdb.HDBModelListener

/*******************************************************************************
* Copyright 2009, 2010 Innovation Gate GmbH. All Rights Reserved.
*
* This file is part of the OpenWGA server platform.
*
* OpenWGA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition, a special exception is granted by the copyright holders
* of OpenWGA called "OpenWGA plugin exception". You should have received
* a copy of this exception along with OpenWGA in file COPYING.
* If not, see <http://www.openwga.com/gpl-plugin-exception>.
*
* OpenWGA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenWGA in file COPYING.
* If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

package de.innovationgate.wgpublisher.hdb;

import java.util.HashMap;
import java.util.Map;

import de.innovationgate.utils.UIDGenerator;
import de.innovationgate.webgate.api.WGAPIException;
import de.innovationgate.webgate.api.WGCSSJSModule;
import de.innovationgate.webgate.api.WGContent;
import de.innovationgate.webgate.api.WGDocumentKey;
import de.innovationgate.webgate.api.WGHierarchicalDatabaseEvent;
import de.innovationgate.webgate.api.WGHierarchicalDatabaseEventException;
import de.innovationgate.webgate.api.WGHierarchicalDatabaseListenerV2;
import de.innovationgate.webgate.api.WGScriptModule;
import de.innovationgate.wgpublisher.WGACore;
import de.innovationgate.wgpublisher.webtml.utils.TMLForm;
import de.innovationgate.wgpublisher.webtml.utils.TMLScriptHDBListener;

public class HDBModelListener implements WGHierarchicalDatabaseListenerV2 {
 
  public static final String TMLSCRIPT_LISTENER_FOLDER = "hdbmodel";
   
    private HDBModel getModel(WGHierarchicalDatabaseEvent event) {
        return (HDBModel) event.getDb().getWrappedDB().getAttribute(WGACore.DBATTRIB_HDBMODEL);
    }
   
    public String getName() {
        return "HDB Model Listener";
    }

    public void postMoveContentFrom(WGHierarchicalDatabaseEvent event) throws Throwable {
      if (!(event.getParameter() instanceof HDBModelParams)) {
            // Exit. This is no hdb model triggered action
            return;
        }
       
        // If the listener is a hdbmodel content we will remove it's relation on the created content
        WGContent listenerContent = event.getListenerContent();
        WGContent movedContent = event.getContent();
        if (listenerContent.getItemValue(HDBModel.ITEM_TYPE).equals(HDBModel.TYPE_CONTENT)) {
            String relationName = "parent-" + listenerContent.getContentClass();
            movedContent.removeRelation(relationName);
            movedContent.save();
        }
       
       
      if (event.isDirectChild()) {
           TMLScriptHDBListener customListener = retrieveCustomListener(event);
           if (customListener != null) {
             customListener.postMoveContentFrom(event);
           }       
       }
    }

    public void postMoveContentTo(WGHierarchicalDatabaseEvent event) throws Throwable {
      if (!(event.getParameter() instanceof HDBModelParams)) {
            // Exit. This is no hdb model triggered action
            return;
        }
       
        // If the listener is a hdbmodel content we will create a relation to it on the created content
        WGContent listenerContent = event.getListenerContent();
        WGContent movedContent = event.getContent();
        if (listenerContent.getItemValue(HDBModel.ITEM_TYPE).equals(HDBModel.TYPE_CONTENT)) {
            String relationName = "parent-" + listenerContent.getContentClass();
            movedContent.setRelation(relationName, listenerContent);           
            movedContent.save();
        }
       
      if (event.isDirectChild()) {
           TMLScriptHDBListener customListener = retrieveCustomListener(event);
           if (customListener != null) {
             customListener.postMoveContentTo(event);
           }       
       }
    }

    public void preMoveContentFrom(WGHierarchicalDatabaseEvent event) throws Throwable {
      if (!(event.getParameter() instanceof HDBModelParams)) {
            // Exit. This is no hdb model triggered action
            return;
        }
       
      if (event.isDirectChild()) {
           TMLScriptHDBListener customListener = retrieveCustomListener(event);
           if (customListener != null) {
             customListener.preMoveContentFrom(event);
           }       
       }
    }

    public void preMoveContentTo(WGHierarchicalDatabaseEvent event) throws Throwable {
      if (!(event.getParameter() instanceof HDBModelParams)) {
            // Exit. This is no hdb model triggered action
            return;
        }
     
     
      if (event.isDirectChild()) {
           TMLScriptHDBListener customListener = retrieveCustomListener(event);
           if (customListener != null) {
             customListener.preMoveContentTo(event);
           }       
       }
    }

    public void postCreateContent(WGHierarchicalDatabaseEvent event) throws Throwable {

      if (!(event.getParameter() instanceof HDBModelParams)) {
            // Exit. This is no hdb model triggered action
            return;
        }
       
        HDBModelParams params = (HDBModelParams) event.getParameter();
       
        HDBModel model = getModel(event);
        WGContent newContent = event.getContent();
        boolean saveContent = false;
        WGContent listenerContent = event.getListenerContent();
       
        // Operations for the parent of the created content only (should only be executed once)
        WGContent parentContent = event.getParentContent();
        if (parentContent != null && parentContent.equals(event.getListenerContent())) {
           
            newContent.setItemValue(HDBModel.ITEM_TYPE, params.getType());
           
            // Operations for model contents only
            if (params.getType().equals(HDBModel.TYPE_CONTENT)) {
               
                // Initialize the content itself
                newContent.setContentClass(params.getContentClass());
                event.getDb().setListener(newContent, getClass().getName()); // Only on content. On storages this is done by HDBModel.initModel()
               
                model.getHdb().assignContentUID(newContent, params.getCreateContentID());
                newContent.setItemValue(HDBModel.ITEM_ID, params.getCreateContentID());

                // Transfer eventual form data
                TMLForm form = params.getForm();
                if (form != null) {
                    form.transfertodocument(newContent);
                    form.attach(event.getContent());
                }
               
                // We need to save here once, so submodel initialisations have access to the data on this content
                newContent.save();
               
                // Init the submodel of a new content
                if (event.getContent().getItemText(HDBModel.ITEM_TYPE).equals(HDBModel.TYPE_CONTENT)) {
                    model.initSubmodel(newContent);
                }
               
                 TMLScriptHDBListener customListener = retrieveCustomListener(event);
              if (customListener != null) {
                customListener.postCreateContent(event);
              }       
             
            }
           
            // Operations for model storages only
            else if (params.getType().equals(HDBModel.TYPE_STORAGE)) {
                newContent.setContentClass("$hdbmodel-storage");
            }

            saveContent = true;
        }
       
        // If the listener is a hdbmodel content we will create a relation to it on the created content
        if (listenerContent.getItemValue(HDBModel.ITEM_TYPE).equals(HDBModel.TYPE_CONTENT)) {
            String relationName = "parent-" + listenerContent.getContentClass();
            newContent.setRelation(relationName, listenerContent);           
            saveContent = true;
        }
       
        if (saveContent) {
            newContent.save();
        }
    }
       
   

    public void postDeleteContent(WGHierarchicalDatabaseEvent event) throws Throwable {
    }

    public void postUpdateContent(WGHierarchicalDatabaseEvent event) throws Throwable {
      if (!(event.getParameter() instanceof HDBModelParams)) {
            // Exit. This is no hdb model triggered action
            return;
        }
     
        if (event.isDirectChild()) {
          TMLScriptHDBListener customListener = retrieveCustomListener(event);
          if (customListener != null) {
            customListener.postUpdateContent(event);
          }       
      }
     
    }

    public void preCreateContent(WGHierarchicalDatabaseEvent event) throws Throwable {
        if (!(event.getParameter() instanceof HDBModelParams)) {
            // Exit. This is no hdb model triggered action
            return;
        }
       
        HDBModelParams params = (HDBModelParams) event.getParameter();
       
        if (event.isDirectChild()) {
          TMLScriptHDBListener customListener = retrieveCustomListener(event, params.getContentClass());
          if (customListener != null) {
            customListener.preCreateContent(event);
          }       
      }
       
        TMLForm form = params.getForm();
        if (form != null) {
            // validate form
            if (!form.validate()) {
                form.gettargetcontext().addwarning("Form validation during storeindocument() failed. Formdata not saved.", false);
                event.cancel("Form validation failed");
            }       
         }
       
    }

    public void preDeleteContent(WGHierarchicalDatabaseEvent event) throws Throwable {
      if (!(event.getParameter() instanceof HDBModelParams)) {
            // Exit. This is no hdb model triggered action
            return;
        }
       
      if (event.isDirectChild()) {
          TMLScriptHDBListener customListener = retrieveCustomListener(event);
          if (customListener != null) {
            customListener.preDeleteContent(event);
          }       
      }
     
    }

    public void preUpdateContent(WGHierarchicalDatabaseEvent event) throws Throwable {
      if (!(event.getParameter() instanceof HDBModelParams)) {
            // Exit. This is no hdb model triggered action
            return;
        }
               
      // Operations for the parent of the created content only (should only be executed once)
        if (event.isDirectChild()) {  
          HDBModelParams params = (HDBModelParams) event.getParameter();
         
          TMLScriptHDBListener customListener = retrieveCustomListener(event);
          if (customListener != null) {
            customListener.preUpdateContent(event);
          }
         
          if (params.getProcess() != null) {
            Map<String, Object> processParams = new HashMap<String, Object>();
            processParams.put("hdbEvent", "event");
            params.getProcess().run(event.getContent(), processParams);
          }
           
            TMLForm form = params.getForm();
            if (form != null) {
                // validate form
                if (!form.validate()) {
                    form.gettargetcontext().addwarning("Form validation during storeindocument() failed. Formdata not saved.", false);
                    event.cancel("Form validation failed");
                    return;
                }       

                // Transfer form data
                form.transfertodocument(event.getContent());
            }
        }
     
    }
   
    private TMLScriptHDBListener retrieveCustomListener(WGHierarchicalDatabaseEvent event, String contentClass) throws WGAPIException {
      String moduleName = TMLSCRIPT_LISTENER_FOLDER + ":" + contentClass;
        WGCSSJSModule module = event.getDb().getWrappedDB().getScriptModule(moduleName, WGScriptModule.CODETYPE_TMLSCRIPT);
        if (module != null) {
          return new TMLScriptHDBListener(getModel(event).getCore(), moduleName);
        }
        return null;
    }
   
    private TMLScriptHDBListener retrieveCustomListener(WGHierarchicalDatabaseEvent event) throws WGAPIException {
      return retrieveCustomListener(event, event.getContent().getContentClass());
    }

}
TOP

Related Classes of de.innovationgate.wgpublisher.hdb.HDBModelListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.