util.WebUtil.java Source code

Java tutorial

Introduction

Here is the source code for util.WebUtil.java

Source

/**
* Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org]
*
* This file is part of Redbasin OpenDocShare community project.
*
* Redbasin OpenDocShare 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

package util;

/**
* Author: Smitha Gudur
* FileName: WebUtil.java
* It provides methods for calendar 
*/

import java.io.*;
import java.text.NumberFormat;
import java.text.ParseException;
import java.awt.Dimension;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Smitha Gudur (smitha@redbasin.com)
 * @version $Revision: 1.3 $
 */
public class WebUtil {

    protected final Log logger = LogFactory.getLog(getClass());
    private final static int numSpaces = 4;
    private final static String htmlSpace = "&nbsp;";

    /**
     * This method returns the other color
     * @param color1
     * @param color2
     * @param lastColor
     * @return String
     */
    public static String toggleColor(String color1, String color2, String lastColor) {
        if (lastColor.equalsIgnoreCase(color1)) {
            return color2;
        }
        return color1;
    }

    /**
     * given a zone return the string representation of zone.
     * for example: -8 returns Pacific
     */
    public static String getZoneStr(String zoneStr) {
        if (RegexStrUtil.isNull(zoneStr)) {
            return null;
        }

        // case -8:
        if (zoneStr.equals(GlobalConst.PacificZone)) {
            return GlobalConst.Pacific;
        }
        //case -7:
        if (zoneStr.equals(GlobalConst.MountainZone)) {
            return GlobalConst.Mountain;
        }
        // case -6:
        if (zoneStr.equals(GlobalConst.CentralZone)) {
            return GlobalConst.Central;
        }
        // case -5:
        if (zoneStr.equals(GlobalConst.EasternZone)) {
            return GlobalConst.Eastern;
        }
        // case -4:
        if (zoneStr.equals(GlobalConst.AtlanticZone)) {
            return GlobalConst.Atlantic;
        }
        // case  -9:
        if (zoneStr.equals(GlobalConst.AlaskanZone)) {
            return GlobalConst.Alaskan;
        }

        // case -10:
        if (zoneStr.equals(GlobalConst.HawaiianZone)) {
            return GlobalConst.Hawaiian;
        }

        // case 0:
        if (zoneStr.equals(GlobalConst.GMTZone)) {
            return GlobalConst.GMT;
        }

        // case 1:
        if (zoneStr.equals(GlobalConst.WEuropeZone)) {
            return GlobalConst.WEurope;
        }

        //case 2:
        if (zoneStr.equals(GlobalConst.EEuropeZone)) {
            return GlobalConst.EEurope;
        }

        // case 10:
        if (zoneStr.equals(GlobalConst.EAustraliaZone)) {
            return GlobalConst.EAustralia;
        }

        // case -12:
        if (zoneStr.equals(GlobalConst.DatelineZone)) {
            return GlobalConst.Dateline;
        }

        // case -11:
        if (zoneStr.equals(GlobalConst.SamoaZone)) {
            return GlobalConst.Samoa;
        }

        // case -2:
        if (zoneStr.equals(GlobalConst.MidAtlanticZone)) {
            return GlobalConst.MidAtlantic;
        }

        //case -1:
        if (zoneStr.equals(GlobalConst.AzoresZone)) {
            return GlobalConst.Azores;
        }

        // case 4:
        if (zoneStr.equals(GlobalConst.ArabianZone)) {
            return GlobalConst.Arabian;
        }

        // case 5:
        if (zoneStr.equals(GlobalConst.WAsiaZone)) {
            return GlobalConst.WAsia;
        }
        // case 6:
        if (zoneStr.equals(GlobalConst.CAsiaZone)) {
            return GlobalConst.CAsia;
        }

        //case 7:
        if (zoneStr.equals(GlobalConst.ThailandZone)) {
            return GlobalConst.Thailand;
        }

        // case 9.5:
        if (zoneStr.equals(GlobalConst.CAustraliaZone)) {
            return GlobalConst.CAustralia;
        }

        //case 5.5:
        if (zoneStr.equals(GlobalConst.IndiaZone)) {
            return GlobalConst.India;
        }

        //-3.5:
        if (zoneStr.equals(GlobalConst.NewfoundlandZone)) {
            return GlobalConst.Newfoundland;
        }

        // 3.5:
        if (zoneStr.equals(GlobalConst.IranZone)) {
            return GlobalConst.Iran;
        }

        // 7V
        if (zoneStr.equals(GlobalConst.VietnamZone)) {
            return GlobalConst.Vietnam;
        }
        //12F:
        if (zoneStr.equals(GlobalConst.FijiZone)) {
            return GlobalConst.Fiji;
        }
        //3S:
        if (zoneStr.equals(GlobalConst.SaudiArabiaZone)) {
            return GlobalConst.SaudiArabia;
        }
        //3R:
        if (zoneStr.equals(GlobalConst.RussiaZone)) {
            return GlobalConst.Russia;
        }
        //12N:
        if (zoneStr.equals(GlobalConst.NewZealandZone)) {
            return GlobalConst.NewZealand;
        }
        //-3B:
        if (zoneStr.equals(GlobalConst.BrazilZone)) {
            return GlobalConst.Brazil;
        }
        // -3A:
        if (zoneStr.equals(GlobalConst.ArgentinaZone)) {
            return GlobalConst.Argentina;
        }
        // 7I:
        if (zoneStr.equals(GlobalConst.IndonesiaZone)) {
            return GlobalConst.Indonesia;
        }
        // 8c:
        if (zoneStr.equals(GlobalConst.ChinaZone)) {
            return GlobalConst.China;
        }
        //8S:
        if (zoneStr.equals(GlobalConst.SingaporeZone)) {
            return GlobalConst.Singapore;
        }
        //8T:
        if (zoneStr.equals(GlobalConst.TaiwanZone)) {
            return GlobalConst.Taiwan;
        }
        // 9J:
        if (zoneStr.equals(GlobalConst.JapanZone)) {
            return GlobalConst.Japan;
        }

        // 9K:
        if (zoneStr.equals(GlobalConst.KoreaZone)) {
            return GlobalConst.Korea;
        }
        return " ";
    }

    public static String getIndent(String lvl) {
        StringBuffer sb = new StringBuffer();
        int level = 0;
        try {
            level = new Integer(lvl).intValue();
        } catch (Exception e) {
            // avoiding NFE do nothing, default is 0
        }
        for (int i = 0; i < level; i++) {
            for (int j = 0; j < numSpaces; j++) {
                sb.append(htmlSpace);
            }
        }
        return sb.toString();
    }

    public static String[] getDirPathVals(String dirpath) {
        if (!RegexStrUtil.isNull(dirpath)) {
            return (dirpath.split(DbConstants.DIRPATH_COLON));
        }
        return null;
    }

    public static int getMinRange(int numBlobStreams, int numPhotosPerPage, int pageNum) {

        int minRange = 0;

        if (numBlobStreams > numPhotosPerPage) {
            if (pageNum > 1) {
                minRange = numPhotosPerPage * (pageNum - 1);
            }
            if (minRange > numBlobStreams) {
                minRange = numBlobStreams;
            }
        }
        return minRange;
    }

    public static int getMaxRange(int numBlobStreams, int numPhotosPerPage, int pageNum) {
        int maxRange = numBlobStreams;
        if (numBlobStreams > numPhotosPerPage) {
            maxRange = numPhotosPerPage * pageNum;
            if (maxRange > numBlobStreams) {
                maxRange = numBlobStreams;
            }
        }
        return maxRange;
    }

    /**
     *  isLicenseProfessional
     */
    public static boolean isLicenseProfessional(String sessionlogin) {
        if (!RegexStrUtil.isNull(GlobalConst.License)) {
            boolean isLicenseProfessional = GlobalConst.License.equals(DbConstants.PROFESSIONAL);
            if (!RegexStrUtil.isNull(sessionlogin)) {
                if (!isLicenseProfessional) {
                    if (DiaryAdmin.isDiaryAdmin(sessionlogin)) {
                        return true;
                    }
                }
            }
            return isLicenseProfessional;
        }
        return false; // default set to deluxe
    }

    /**
     *  isRegistrationShown - show the register screen for products.
     *  show register screen for products only for portal and businesscommuity.
     */
    public static boolean isRegistrationShown() {

        if (!RegexStrUtil.isNull(GlobalConst.Product)) {
            if ((GlobalConst.Product.equals(GlobalConst.RedbasinPortalDataCenter))
                    || GlobalConst.Product.equals(GlobalConst.RedbasinBusinessCommunityDataCenter)) {
                return true;
            }
        }
        return false;
    }

    /**
     *  isProductPremimumSubscription - returns false otherwise.
     *  Allows whether the user can access another user's published page.
     *  They are allowed to access another user's page only if they belong to the same biz account
     */
    public static boolean isProductPremiumSubscription() {
        if (!RegexStrUtil.isNull(GlobalConst.Product)) {
            return GlobalConst.Product.equals(GlobalConst.RedbasinPremiumSubscriptionDataCenter);
        }
        return false;
    }

    /**
     *  isProductRedbasinPortalDataCenter - returns false otherwise.
     *  This enables a check for session, if the product is not portal
     */
    public static boolean isProductRedbasinPortalDataCenter() {
        if (!RegexStrUtil.isNull(GlobalConst.Product)) {
            return GlobalConst.Product.equals(GlobalConst.RedbasinPortalDataCenter);
        }
        return false;
    }

    /**
     *  isGeoSmart - returns if geosmart is enabled.
     *  
     */
    public static boolean isGeoSmart() {
        if (!RegexStrUtil.isNull(GlobalConst.GeoSmart)) {
            return GlobalConst.GeoSmart.equals("1");
        }
        return false;
    }

    public static boolean isIE(HttpServletRequest request) {
        String ua = RegexStrUtil.goodText(request.getHeader("User-Agent"));
        if (ua != null) {
            return (ua.indexOf("MSIE") != -1); // return true for an IE
        }
        return false;
    }

    public static String getFont(int maxHits, int minHits, int hits) {
        // int totalHits = maxHits - minHits;
        int hitPerc = (hits * 100) / maxHits;
        int fontWeight = 0;
        if (hitPerc > 0) {
            fontWeight = hitPerc * GlobalConst.maxTagFonts / 100;
            if (fontWeight < 8) {
                if (hitPerc % maxHits > 50) {
                    fontWeight++;
                }
            }
        }

        return (getFontSize(fontWeight));
    }

    private static String getFontSize(int fontWeight) {
        switch (fontWeight) {
        case 0:
        case 1:
            // smallest size
            return GlobalConst.TagFontSize1;
        case 2:
            return GlobalConst.TagFontSize2;
        case 3:
            return GlobalConst.TagFontSize3;
        case 4:
            return GlobalConst.TagFontSize4;
        case 5:
            return GlobalConst.TagFontSize5;
        case 6:
            return GlobalConst.TagFontSize6;
        case 7:
            return GlobalConst.TagFontSize7;
        case 8:
            // biggest size
            return GlobalConst.TagFontSize8;
        }
        if (fontWeight > 8) {
            return GlobalConst.TagFontSize8;
        }
        if (fontWeight < 0) {
            return GlobalConst.TagFontSize1;
        }
        return null;
    }

    public static Dimension getDimension(Dimension d, int mw, int mh) {
        int w = (int) d.getWidth();
        int h = (int) d.getHeight();
        int nh = h;
        int nw = w;
        if (w > mw) {
            float r = (float) mw / (float) w;
            nh = (int) ((float) h * (float) r);
            nw = mw;
        }
        if (nh > mh) {
            float r = (float) mh / (float) nh;
            nw = (int) ((float) nw * (float) r);
            nh = mh;
        }
        d.setSize(nw, nh);
        return d;
    }

    public static boolean isLdapActive() {
        return LdapConstants.enableLdap;
    }

    public static boolean isSanEnabled() {
        return SanConstants.enableSan;
    }

    public static boolean isMyFilesDirectory() {
        return GlobalConst.enableMyFilesDirectory;
    }

    public static boolean isPublishContactsEnabled() {
        return GlobalConst.enablePublishContacts;
    }

    public static boolean isAddUserEnabled() {
        return GlobalConst.enableAddUser;
    }

    public static boolean isAddBusinessEnabled() {
        return GlobalConst.enableAddBusiness;
    }

    public static boolean isCollabrumEnabled() {
        return GlobalConst.enableCollabrum;
    }

    public static boolean isFolksonomyEnabled() {
        return GlobalConst.enableFolksonomy;
    }

    public static boolean isForgotPasswordEnabled() {
        return GlobalConst.enableForgotPassword;
    }

    public static boolean isRegisterEnabled() {
        return GlobalConst.enableRegister;
    }

    public static boolean isSendMyInviteEnabled() {
        return GlobalConst.enableSendMyInvite;
    }

    public static boolean isUserDeactivateEnabled() {
        return GlobalConst.enableDeactivateUser;
    }

    public static boolean isMyImagesEnabled() {
        return GlobalConst.enableMyImages;
    }

    public static boolean isMyCobrandEnabled() {
        return GlobalConst.enableMyCobrand;
    }

    public static boolean isDirectoryCobrandEnabled() {
        return GlobalConst.enableDirectoryCobrand;
    }

    public static boolean isDirectoryImagesEnabled() {
        return GlobalConst.enableDirectoryImages;
    }

    public static int getMaxPageNumber(int totalSize, int pageSize) {
        if (totalSize % pageSize == 0) {
            return (totalSize / pageSize);
        } else {
            return ((totalSize / pageSize) + 1);
        }
    }

    public static int getMaxPageSize(int totalSize, int pageSize) {
        if (totalSize > GlobalConst.eighty) {
            return GlobalConst.eighty;
        }
        if (totalSize > GlobalConst.seventy) {
            return GlobalConst.eighty;
        }
        if (totalSize > GlobalConst.sixty) {
            return GlobalConst.seventy;
        }
        if (totalSize > GlobalConst.fifty) {
            return GlobalConst.sixty;
        }
        if (totalSize > GlobalConst.forty) {
            return GlobalConst.fifty;
        }
        if (totalSize > GlobalConst.thirty) {
            return GlobalConst.forty;
        }
        if (totalSize > GlobalConst.twenty) {
            return GlobalConst.thirty;
        }
        if (totalSize > GlobalConst.ten) {
            return GlobalConst.twenty;
        }
        if (totalSize <= GlobalConst.ten) {
            return GlobalConst.ten;
        }
        return GlobalConst.eighty;
    }

    public static boolean isDirTreePasteEnabled() {
        return DirectoryScopeConstants.isDirTreePasteEnabled;
    }

    public static boolean isDirTreeDeleteEnabled() {
        return DirectoryScopeConstants.isDirTreeDeleteEnabled;
    }

    public static boolean isDirTreeRenameEnabled() {
        return DirectoryScopeConstants.isDirTreeRenameEnabled;
    }

    public static boolean isDirTreeCopyEnabled() {
        return DirectoryScopeConstants.isDirTreeCopyEnabled;
    }

    public static boolean isHomeDirDeleteEnabled() {
        return DirectoryScopeConstants.isDirHomeDeleteEnabled;
    }

    public static boolean isFileDetailsDisplayed() {
        return GlobalConst.isFileDetailsDisplayed;
    }

    public static boolean isMyContactsEnabled() {
        return GlobalConst.enableMyContacts;
    }

    public static boolean isQuotaDisplayedinFriends() {
        return GlobalConst.isQuotaDisplayedinFriends;
    }

    /**
    *  isFileMimeTypeExcluded 
    * Enable this function if only certain mimetype(s) are allowed
    * @param mimetype
    * @return 
    */
    public static boolean isFileMimeTypeExcluded(String mimetype) {
        return false;
        /*  enable this code
        if (RegexStrUtil.isNull(mimetype)) {
        return false;
        } else {
        if (GlobalConst.fileMimeTypes != null && GlobalConst.fileMimeTypes.length > 0) {
        for (int i = 0; i < GlobalConst.fileMimeTypes.length; i++) {
        if (!RegexStrUtil.isNull(GlobalConst.fileMimeTypes[i])) {
          if ((mimetype.indexOf(GlobalConst.fileMimeTypes[i]) != -1) || mimetype.equals(GlobalConst.fileMimeTypes[i])) { 
        return true;
        } else {
        continue;
        }
        }
        }
        }
        return false;
        }  */
    }

    /**
     *  isProductSecureBusinessDataCenter - returns false otherwise.
     *  This enables a check for session, if the product is not portal
     */
    public static boolean isProductSecureBusinessDataCenter() {
        if (!RegexStrUtil.isNull(GlobalConst.Product)) {
            return GlobalConst.Product.equals(GlobalConst.RedbasinSecureBusinessDataCenter);
        }
        return false;
    }

    public static boolean isMyBlogsEnabled() {
        return GlobalConst.enableMyBlogs;
    }

    public static boolean isMostPopularEnabled() {
        return GlobalConst.enableMostPopular;
    }

    public static boolean isPasswordEncrypted() {
        return GlobalConst.enableEncryptPassword;
    }

    public static boolean isEncryptionMD5() {
        return GlobalConst.enableEncryptPasswordMD5;
    }

    public static boolean isEncryptionSHA1() {
        return GlobalConst.enableEncryptPasswordSHA1;
    }

    public static boolean isActivateAccountEnabled() {
        return GlobalConst.isActivateAccountEnabled;
    }

    public static boolean isAboutEnabled() {
        return GlobalConst.isAboutEnabled;
    }

    public static boolean isUserQuotaEnabled() {
        return GlobalConst.isUserQuotaEnabled;
    }

    public static boolean isLdapAddUsersEnabled() {
        return LdapConstants.isEnableAddLdapUsers;
    }

    public static boolean isSocialNetworkingEnabled() {
        return GlobalConst.isSocialNetworkingEnabled;
    }

    public static boolean isProfileEnabled() {
        return GlobalConst.isProfileEnabled;
    }

    public static boolean isLuceneSearchEnabled() {
        return LuceneConstants.enableLuceneSearch;
    }
}