// Generated by OABuilder

package com.cdi.model;

import java.util.logging.*;
import com.viaoa.object.*;
import com.viaoa.annotation.*;
import com.viaoa.datasource.*;
import com.viaoa.hub.*;
import com.viaoa.util.*;
import com.viaoa.util.filter.*;
import com.viaoa.model.oa.VInteger;

import com.cdi.model.oa.*;
import com.cdi.model.oa.propertypath.*;
import com.cdi.model.oa.search.*;
import com.cdi.model.oa.filter.*;
import com.cdi.model.search.*;
import com.cdi.model.filter.*;
import com.cdi.delegate.ModelDelegate;
import com.cdi.resource.Resource;

public class WebPartModel extends OAObjectModel {
    private static Logger LOG = Logger.getLogger(WebPartModel.class.getName());
    
    /* overview
      originalHub   - save the original hub
      rootHub       - for recursive, root
      recursiveHub  - will be shared with root or child hub
      flattenedHub  - will merge all recursive child hubs into one
      treeTableHub  - will have root objects and all expanded child hub objects
    */
    
    // types of ways to view the original hub
    public static final int TYPE_Original = 0;
    public static final int TYPE_TreeTable = 1;
    public static final int TYPE_Root = 2;
    public static final int TYPE_Flattened = 3;
    public static final int TYPE_Recursive = 4;
    
    // Hubs
    protected Hub<WebPart> hubOriginal;
    // hubs needed since WebPart is recursive
    protected Hub<WebPart> hubRecursive; // used by tree, points to root or child hub
    protected Hub<WebPart> hubTreeTable; // hub of root and the expanded node objects
    protected Hub<WebPart> hubFlattenedTreeTable; // works with tree
    protected Hub<WebPart> hubRoot;      // recursive root
    protected Hub<WebPart> hubFlattened; // recursive flattened
    
    // main hub that points to hubOriginal, hubRoot, hubRecursive, hubFlattened, hubTreeTable
    protected Hub<WebPart> hub;
    // selected webParts
    protected Hub<WebPart> hubMultiSelect;
    // detail hubs
    protected Hub<ImageStore> hubImageStore;
    protected Hub<WebPage> hubLinkToWebPage;
    protected Hub<WebPart> hubParentWebPart;
    protected Hub<WebPage> hubWebPage;
    protected Hub<WebItem> hubWebItems;
    protected Hub<WebPart> hubWebParts;
    
    // ObjectModels
    protected ImageStoreModel modelImageStore;
    protected WebPageModel modelLinkToWebPage;
    protected WebPartModel modelParentWebPart;
    protected WebPageModel modelWebPage;
    protected WebItemModel modelWebItems;
    protected WebPartModel modelWebParts;
    
    // SearchModels used for references
    protected ImageStoreSearchModel modelImageStoreSearch;
    
    public WebPartModel() {
        setDisplayName("Web Part");
        setPluralDisplayName("Web Parts");
        setAllowRecursive(true);
    }
    
    public WebPartModel(Hub<WebPart> hubWebPart) {
        this();
        if (hubWebPart != null) HubDelegate.setObjectClass(hubWebPart, WebPart.class);
        this.hubOriginal = hubWebPart;
        setType(TYPE_Original);
    }
    public WebPartModel(WebPart webPart) {
        this();
        getOriginalHub().add(webPart);
        getOriginalHub().setPos(0);
        setType(TYPE_Original);
    }
    
    public WebPartModel(Hub<WebPart> hubRootWebPart, Hub<WebPart> hubWebPart) {
        this();
        hubOriginal = hubRootWebPart;
        hubRecursive = hubWebPart;
        setType(TYPE_Recursive);
    }
    
    // the type of hub that is active
    public void setType(int type) {
        updateType(type);
        getVType().setValue(type);
    }
    public int getType() {
        return getVType().getValue();
    }
    // listen to type change
    public void addTypeListener(HubListener hl) {
        if (hl != null) getTypeHub().addHubListener(hl);
    }
    // used to listen to type change
    private Hub<VInteger> hubType;
    private VInteger type;
    private VInteger getVType() {
        if (type == null) {
            type = new VInteger();
        }
        return type;
    }
    private Hub<VInteger> getTypeHub() {
        if (hubType == null) {
            hubType = new Hub<VInteger>(VInteger.class);
            hubType.add(getVType());
            hubType.setPos(0);
        }
        return hubType;
    }
    private void updateType(int newValue) {
        if (!getAllowRecursive()) newValue = TYPE_Original;
        switch (newValue) {
        case TYPE_Original:
            getHub().setSharedHub(getOriginalHub(), true);
            break;
        case TYPE_TreeTable:
            getHub().setSharedHub(getFlattenedTreeTableHub(), true);
            break;
        case TYPE_Root:
            getHub().setSharedHub(getRootHub(), true);
            break;
        case TYPE_Flattened:
            getHub().setSharedHub(getFlattenedHub(), true);
            break;
        case TYPE_Recursive:
            getHub().setSharedHub(getRecursiveHub(), true);
            break;
        }
    }
    
    public Hub<WebPart> getOriginalHub() {
        if (hubOriginal == null) {
            hubOriginal = new Hub<WebPart>(WebPart.class);
        }
        return hubOriginal;
    }
    
    // the root hub of a recursive hub
    public Hub<WebPart> getRootHub() {
        if (hubRoot == null) {
            hubRoot = new Hub<WebPart>(WebPart.class);
            new HubRoot(getOriginalHub(), hubRoot);
        }
        return hubRoot;
    }
    
    // This will point/share to the root or child hub that is active.
    public Hub<WebPart> getRecursiveHub() {
        if (hubRecursive == null) {
            hubRecursive = new Hub<WebPart>(WebPart.class);
            hubRecursive.setSharedHub(getOriginalHub());
        }
        return hubRecursive;
    }
    
    // flattened hub for a recursive hub
    public Hub<WebPart> getFlattenedHub() {
        if (hubFlattened == null) {
            hubFlattened = new Hub<WebPart>(WebPart.class);
            HubFlattened hf = new HubFlattened(getOriginalHub(), hubFlattened);
        }
        return hubFlattened;
    }
    
    // hub of expanded nodes in treeTable
    public Hub<WebPart> getTreeTableHub() {
        if (hubTreeTable == null) {
            hubTreeTable = new Hub<WebPart>(WebPart.class);
        }
        return hubTreeTable;
    }
    public Hub<WebPart> getFlattenedTreeTableHub() {
        if (hubFlattenedTreeTable == null) {
            hubFlattenedTreeTable = new Hub<WebPart>(WebPart.class);
            new HubFlattened(getOriginalHub(), hubFlattenedTreeTable);
        }
        return hubFlattenedTreeTable;
    }
    
    public Hub<ImageStore> getImageStoreHub() {
        if (hubImageStore != null) return hubImageStore;
        hubImageStore = getHub().getDetailHub(WebPart.P_ImageStore);
        return hubImageStore;
    }
    public Hub<WebPage> getLinkToWebPageHub() {
        if (hubLinkToWebPage != null) return hubLinkToWebPage;
        hubLinkToWebPage = new Hub<>(WebPage.class);
        Hub<WebPage> hub = ModelDelegate.getWebPages();
        HubCopy<WebPage> hc = new HubCopy<>(hub, hubLinkToWebPage, false);
        hubLinkToWebPage.setLinkHub(getHub(), WebPart.P_LinkToWebPage); 
        return hubLinkToWebPage;
    }
    public Hub<WebPart> getParentWebPartHub() {
        if (hubParentWebPart != null) return hubParentWebPart;
        hubParentWebPart = getHub().getDetailHub(WebPart.P_ParentWebPart);
        return hubParentWebPart;
    }
    public Hub<WebPage> getWebPageHub() {
        if (hubWebPage != null) return hubWebPage;
        // this is the owner, use detailHub
        hubWebPage = getHub().getDetailHub(WebPart.P_WebPage);
        return hubWebPage;
    }
    public Hub<WebItem> getWebItems() {
        if (hubWebItems == null) {
            hubWebItems = getHub().getDetailHub(WebPart.P_WebItems);
        }
        return hubWebItems;
    }
    public Hub<WebPart> getWebParts() {
        if (hubWebParts == null) {
            hubWebParts = getHub().getDetailHub(WebPart.P_WebParts);
        }
        return hubWebParts;
    }
    public WebPart getWebPart() {
        return getHub().getAO();
    }
    
    // points to one: hubOriginal, hubFlattened, hubRecursive, hubTreeTable
    public Hub<WebPart> getHub() {
        if (hub == null) {
            hub = new Hub<WebPart>(WebPart.class);
            hub.setSharedHub(getOriginalHub(), true);
        }
        return hub;
    }
    
    public Hub<WebPart> getMultiSelectHub() {
        if (hubMultiSelect == null) {
            hubMultiSelect = new Hub<WebPart>(WebPart.class);
        }
        return hubMultiSelect;
    }
    
    public ImageStoreModel getImageStoreModel() {
        if (modelImageStore != null) return modelImageStore;
        modelImageStore = new ImageStoreModel(getImageStoreHub());
        modelImageStore.setDisplayName("Image Store");
        modelImageStore.setPluralDisplayName("Images");
        modelImageStore.setForJfc(getForJfc());
        modelImageStore.setAllowNew(true);
        modelImageStore.setAllowSave(true);
        modelImageStore.setAllowAdd(false);
        modelImageStore.setAllowRemove(true);
        modelImageStore.setAllowClear(true);
        modelImageStore.setAllowDelete(false);
        modelImageStore.setAllowSearch(true);
        modelImageStore.setAllowHubSearch(true);
        modelImageStore.setAllowGotoEdit(true);
        modelImageStore.setViewOnly(getViewOnly());
        // call WebPart.onEditQueryImageStore(ImageStoreModel) to be able to customize this model
        OAObjectCallbackDelegate.onObjectCallbackModel(WebPart.class, WebPart.P_ImageStore, modelImageStore);
    
        return modelImageStore;
    }
    public WebPageModel getLinkToWebPageModel() {
        if (modelLinkToWebPage != null) return modelLinkToWebPage;
        modelLinkToWebPage = new WebPageModel(getLinkToWebPageHub());
        modelLinkToWebPage.setDisplayName("Web Page");
        modelLinkToWebPage.setPluralDisplayName("Web Pages");
        modelLinkToWebPage.setForJfc(getForJfc());
        modelLinkToWebPage.setAllowNew(false);
        modelLinkToWebPage.setAllowSave(true);
        modelLinkToWebPage.setAllowAdd(false);
        modelLinkToWebPage.setAllowRemove(false);
        modelLinkToWebPage.setAllowClear(false);
        modelLinkToWebPage.setAllowDelete(false);
        modelLinkToWebPage.setAllowSearch(false);
        modelLinkToWebPage.setAllowHubSearch(true);
        modelLinkToWebPage.setAllowGotoEdit(false);
        OALinkInfo li = HubDetailDelegate.getLinkInfoFromDetailToMaster(getOriginalHub());
        modelLinkToWebPage.setCreateUI(li == null || !WebPart.P_LinkToWebPage.equals(li.getName()) );
        modelLinkToWebPage.setViewOnly(true);
        // call WebPart.onEditQueryLinkToWebPage(WebPageModel) to be able to customize this model
        OAObjectCallbackDelegate.onObjectCallbackModel(WebPart.class, WebPart.P_LinkToWebPage, modelLinkToWebPage);
    
        return modelLinkToWebPage;
    }
    public WebPartModel getParentWebPartModel() {
        if (modelParentWebPart != null) return modelParentWebPart;
        modelParentWebPart = new WebPartModel(getParentWebPartHub());
        modelParentWebPart.setDisplayName("Parent Web Part");
        modelParentWebPart.setPluralDisplayName("Web Parts");
        modelParentWebPart.setForJfc(getForJfc());
        modelParentWebPart.setAllowNew(false);
        modelParentWebPart.setAllowSave(true);
        modelParentWebPart.setAllowAdd(false);
        modelParentWebPart.setAllowRemove(false);
        modelParentWebPart.setAllowClear(false);
        modelParentWebPart.setAllowDelete(false);
        modelParentWebPart.setAllowSearch(false);
        modelParentWebPart.setAllowHubSearch(true);
        modelParentWebPart.setAllowGotoEdit(true);
        OALinkInfo li = HubDetailDelegate.getLinkInfoFromDetailToMaster(getOriginalHub());
        modelParentWebPart.setCreateUI(li == null || !WebPart.P_ParentWebPart.equals(li.getName()) );
        modelParentWebPart.setViewOnly(true);
        // call WebPart.onEditQueryParentWebPart(WebPartModel) to be able to customize this model
        OAObjectCallbackDelegate.onObjectCallbackModel(WebPart.class, WebPart.P_ParentWebPart, modelParentWebPart);
    
        return modelParentWebPart;
    }
    public WebPageModel getWebPageModel() {
        if (modelWebPage != null) return modelWebPage;
        modelWebPage = new WebPageModel(getWebPageHub());
        modelWebPage.setDisplayName("Web Page");
        modelWebPage.setPluralDisplayName("Web Pages");
        modelWebPage.setForJfc(getForJfc());
        modelWebPage.setAllowNew(false);
        modelWebPage.setAllowSave(true);
        modelWebPage.setAllowAdd(false);
        modelWebPage.setAllowRemove(false);
        modelWebPage.setAllowClear(false);
        modelWebPage.setAllowDelete(false);
        modelWebPage.setAllowSearch(false);
        modelWebPage.setAllowHubSearch(true);
        modelWebPage.setAllowGotoEdit(false);
        OALinkInfo li = HubDetailDelegate.getLinkInfoFromDetailToMaster(getOriginalHub());
        modelWebPage.setCreateUI(li == null || !WebPart.P_WebPage.equals(li.getName()) );
        modelWebPage.setViewOnly(getViewOnly());
        // call WebPart.onEditQueryWebPage(WebPageModel) to be able to customize this model
        OAObjectCallbackDelegate.onObjectCallbackModel(WebPart.class, WebPart.P_WebPage, modelWebPage);
    
        return modelWebPage;
    }
    public WebItemModel getWebItemsModel() {
        if (modelWebItems != null) return modelWebItems;
        modelWebItems = new WebItemModel(getWebItems());
        modelWebItems.setDisplayName("Web Item");
        modelWebItems.setPluralDisplayName("Web Items");
        if (HubDetailDelegate.getLinkInfoFromMasterToDetail(getOriginalHub().getMasterHub()) == HubDetailDelegate.getLinkInfoFromMasterToDetail(getWebItems())) {
            modelWebItems.setCreateUI(false);
        }
        modelWebItems.setForJfc(getForJfc());
        modelWebItems.setAllowNew(false); // turned off and replaced by allowAdd=true, so user must first pick Item
        modelWebItems.setAllowSave(true);
        modelWebItems.setAllowAdd(true); // Add is overwritten to create new WebItem by first selecting Item
        modelWebItems.setAllowMove(true);
        modelWebItems.setAllowRemove(false);
        modelWebItems.setAllowDelete(true);
        modelWebItems.setAllowSearch(false);
        modelWebItems.setAllowHubSearch(true);
        modelWebItems.setAllowGotoEdit(true);
        modelWebItems.setViewOnly(getViewOnly());
        modelWebItems.setAllowTableFilter(false);
        modelWebItems.setAllowTableSorting(false);
         // default is always false for these, can be turned by custom code in editQuery call (below)
        modelWebItems.setAllowMultiSelect(false);
        modelWebItems.setAllowCopy(false);
        modelWebItems.setAllowCut(false);
        modelWebItems.setAllowPaste(false);
        // call WebPart.onEditQueryWebItems(WebItemModel) to be able to customize this model
        OAObjectCallbackDelegate.onObjectCallbackModel(WebPart.class, WebPart.P_WebItems, modelWebItems);
    
        return modelWebItems;
    }
    public WebPartModel getWebPartsModel() {
        if (modelWebParts != null) return modelWebParts;
        modelWebParts = new WebPartModel(getWebParts());
        modelWebParts.setDisplayName("Web Part");
        modelWebParts.setPluralDisplayName("Web Parts");
        if (HubDetailDelegate.getLinkInfoFromMasterToDetail(getOriginalHub().getMasterHub()) == HubDetailDelegate.getLinkInfoFromMasterToDetail(getWebParts())) {
            modelWebParts.setCreateUI(false);
        }
        modelWebParts.setForJfc(getForJfc());
        modelWebParts.setAllowNew(true);
        modelWebParts.setAllowSave(true);
        modelWebParts.setAllowAdd(false);
        modelWebParts.setAllowMove(true);
        modelWebParts.setAllowRemove(false);
        modelWebParts.setAllowDelete(true);
        modelWebParts.setAllowSearch(false);
        modelWebParts.setAllowHubSearch(true);
        modelWebParts.setAllowGotoEdit(true);
        modelWebParts.setViewOnly(getViewOnly());
        modelWebParts.setAllowTableFilter(false);
        modelWebParts.setAllowTableSorting(false);
        modelWebParts.setAllowRecursive(true);
         // default is always false for these, can be turned by custom code in editQuery call (below)
        modelWebParts.setAllowMultiSelect(false);
        modelWebParts.setAllowCopy(false);
        modelWebParts.setAllowCut(false);
        modelWebParts.setAllowPaste(false);
        // call WebPart.onEditQueryWebParts(WebPartModel) to be able to customize this model
        OAObjectCallbackDelegate.onObjectCallbackModel(WebPart.class, WebPart.P_WebParts, modelWebParts);
    
        return modelWebParts;
    }
    
    public ImageStoreSearchModel getImageStoreSearchModel() {
        if (modelImageStoreSearch != null) return modelImageStoreSearch;
        modelImageStoreSearch = new ImageStoreSearchModel();
        return modelImageStoreSearch;
    }
    
    public HubCopy<WebPart> createHubCopy() {
        Hub<WebPart> hubWebPartx = new Hub<>(WebPart.class);
        HubCopy<WebPart> hc = new HubCopy<>(getHub(), hubWebPartx, true);
        return hc;
    }
    public WebPartModel createCopy() {
        WebPartModel mod = new WebPartModel(createHubCopy().getHub());
        return mod;
    }
}

