// Copyright (c) 2001, Oracle Corporation. All rights reserved.
package oracle.portal.provider.v2;

import java.util.StringTokenizer;

public class MobileFlags
{

    /**
     *  Flags that define the portlet's mobile settings.  OR these bits
     *  together to create unions.
     *  <p>
     *  <code>MOBILE_GENERIC</code>: placeholder that means this is a default
     *  (or generic) portlet.<p>
     *  <code>MOBILE_ONLY</code>: this flag indicates the portlet shouldn't be
     *  rendered in the desktop viewer.  I.e. the portal shouldn't transform
     *  this portlet's mobileXML to HTML. <p>
     *  <code>EXTERNAL_SHOW</code>: this flag indicates the portlet's show link is
     *  "external".  I.e. rather then use the page URL to generate a portal
     *  relative link to the portlet, an absolute (external to the portal) link
     *  is generated.  These types of portlets tyipcally won't render when
     *  accessed from the mobile viewer.  By setting this flag, the portal can
     *  provide UI that indicates this to the user.
     */
    public static final long MOBILE_GENERIC = 0;
    public static final long MOBILE_ONLY = 1;
    public static final long EXTERNAL_SHOW = 2;

    /**
     *  String constant that is the token name for the associated flag.
     */
    public static final String TOKEN_MOBILE_GENERIC = "MOBILE_GENERIC";

    /**
     *  String constant that is the token name for the associated flag.
     */
    public static final String TOKEN_MOBILE_ONLY = "MOBILE_ONLY";

    /**
     *  String constant that is the token name for the associated flag.
     */
    public static final String TOKEN_EXTERNAL_SHOW = "EXTERNAL_SHOW";

    private long mMobileFlags = MOBILE_GENERIC;

    /**
     * Constructor -- allows you to represent flags as "MOBILE_ONLY | EXTERNAL_SHOW"
     */
    public MobileFlags(String flags) throws IllegalArgumentException
    {
        setValue(flags);
    }

    /**
     * Constructor
     */
    public MobileFlags(long flags)
    {
        setValue(flags);
    }

    /**
     * Constructor
     */
    public MobileFlags(Long flags)
    {
        setValue(flags.longValue());
    }


    /**
     * @return whether the current mobile settings include the MobileOnly flag.
     * MobileOnly tells the portal it shouldn't render this portlet to the
     * desktop (viewer).
     */
    public boolean isMobileOnly()
    {
        return ((mMobileFlags & MOBILE_ONLY) == MOBILE_ONLY);
    }

    /**
     * @return whether the current mobile settings include the ExternalShow flag.
     * ExternalShow tells the portal that link to the portlet is external, not
     * relative to the portal.
     */
    public boolean isExternalShow()
    {
        return ((mMobileFlags & EXTERNAL_SHOW) == EXTERNAL_SHOW);
    }


    /**
     * @return the current MobileFlags value.  This is a bitwise long.
     */
    public long getValue()
    {
        return mMobileFlags;
    }

    /**
     * @param sets the MobileFlags to the bitwise flags represented by the long.
     */
    public void setValue(long flags)
    {
        mMobileFlags = flags;
    }

    /**
     * @param sets the MobileFlags to the bitwise flags represented by the Long.
     */
    public void setValue(Long flags)
    {
        mMobileFlags = flags.longValue();
    }

    /**
     * @param sets the MobileFlags to the bitwise flags represented by the String.
     * Flags in the string are represented by their names separated by a |.
     */
    public void setValue(String flags) throws IllegalArgumentException
    {
        /* first try and convert flags to a long */
        try
        {
            setValue(Long.parseLong(flags));
        } catch (NumberFormatException nfe)
        {
            /* decode the string */
            StringTokenizer tokens = new StringTokenizer(flags, " \t\n\r|");
            if (tokens.countTokens() == 0) throw new IllegalArgumentException();
            long tempFlag = MobileFlags.MOBILE_GENERIC;
            while (tokens.hasMoreTokens())
            {
                String s = tokens.nextToken();
                if (s.equalsIgnoreCase(MobileFlags.TOKEN_MOBILE_ONLY))
                {
                    tempFlag |= MobileFlags.MOBILE_ONLY;
                }
                else if (s.equalsIgnoreCase(MobileFlags.TOKEN_EXTERNAL_SHOW))
                {
                    tempFlag |= MobileFlags.EXTERNAL_SHOW;
                }
                else if (s.equalsIgnoreCase(MobileFlags.TOKEN_MOBILE_GENERIC))
                {
                    // do nothing as we a "generic" be default
                }
                else
                    throw new IllegalArgumentException();
            }
            setValue(tempFlag);
        }
    }
}

 
