All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.empire.jakarta.pages.PageElement Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/*
 * 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.
 */
package org.apache.empire.jakarta.pages;

import java.util.Map;

import jakarta.faces.context.FacesContext;

import org.apache.empire.commons.StringUtils;
import org.apache.empire.db.DBRowSet;
import org.apache.empire.exceptions.InvalidArgumentException;


public class PageElement // *Deprecated* implements Serializable
{
    // *Deprecated* private static final long serialVersionUID = 1L;

    private final Page  page;

    private final String    propertyName;

    protected PageElement(Page page, String propertyName)
    {
        if (page==null)
            throw new InvalidArgumentException("page", page);
        if (StringUtils.isEmpty(propertyName))
            throw new InvalidArgumentException("propertyName", propertyName);
        // set params
        this.page = page;
        this.propertyName = propertyName;
        // register with page
        page.registerPageElement(this);
    }

    public String getPropertyName()
    {
        return propertyName;
    }
    
    public Page getPage()
    {
        return page;
    }

    protected void onInitPage()
    {
        // Chance to init the page
    }

    protected void onRefreshPage()
    {
        // Chance to init the page
    }

    /* Session Object handling */

    @SuppressWarnings("unchecked")
    protected  T getSessionObject(Class type)
    {
        String beanName = page.getPageName() + "." + propertyName + "." + type.getSimpleName();
        Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        return (T) map.get(beanName);
    }

    protected  void setSessionObject(Class type, T object)
    {
        String beanName = page.getPageName() + "." + propertyName + "." + type.getSimpleName();
        Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        if (object != null)
            map.put(beanName, object);
        else
            map.remove(beanName);
    }

    protected  void removeSessionObject(Class type)
    {
        setSessionObject(type, null);
    }

    /*
     * Removed with EMPIREDB-421:
     * 
    public DBContext getDBContext(DBDatabase db)
    {
        return page.getDBContext(db);
    }

    public DBContext getDBContext(DBObject dbo)
    {
        if (dbo==null)
            throw new InvalidArgumentException("dbo", dbo);
        return getDBContext(dbo.getDatabase());
    }
    */
    
    /**
     * generates a default property name for the bean list
     * @param rowset the rowset
     * @return a propertyName
     */
    protected static String getDefaultPropertyName(DBRowSet rowset)
    {
        String name = rowset.getName();
        if (name==null)
            return "unknown"; // no name provided!
        // compute name
        name = name.toLowerCase();        
        String res = "";
        int beg=0;
        while (begbeg)
            {
                if (beg==0)
                {   // begin with all lower cases
                    res = name.substring(beg, end);
                }
                else
                {   // add word where first letter is upper case 
                    res += name.substring(beg, beg+1).toUpperCase();
                    if (end-beg>1)
                    {
                        res += name.substring(beg+1, end);
                    }
                }
            }
            // next
            beg = end + 1;
        }
        // Result
        return res;
    }
    
    /**
     * Javascript Call
     * 
     * @param function
    protected void addJavascriptCall(String function)
    {
        page.addJavascriptCall(function);
    }
     */
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy