SinaburoViewUtil.java 9.61 KB
Newer Older
Kim Gyeongeun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
package jp.agentec.sinaburocast.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import jp.agentec.sinaburocast.common.SinaburoConstant.MesResKey.Errors;
import jp.agentec.sinaburocast.common.exception.SystemException;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.util.MessageResources;
import org.seasar.framework.util.InputStreamUtil;
import org.seasar.framework.util.OutputStreamUtil;
import org.seasar.framework.util.StringUtil;
import org.seasar.struts.util.MessageResourcesUtil;
import org.seasar.struts.util.S2PropertyMessageResources;
import org.seasar.struts.util.S2PropertyMessageResourcesFactory;

/**
 * HTML生成ユーティリティークラス<br>
 * ロジック内でHTMLを生成する場合、他のユーティリティーと判別するため、当クラスに記述する。
 * @author soda
 *
 */
public class SinaburoViewUtil {

	/** aタグフォーマット */
	private static final String HTML_FORMAT_HERF = "<li><a href='%s'>%s</a></li>";
	/** spanタグフォーマット */
	private static final String HTML_FORMAT_SPAN = "<span>%s</span>";
	/** カレントページ */
	private static final String HTML_CURRENT_PAGE_FORMAT = "<li><span><B><font  color='red'>%d</font></B></span></li>";
	/** ページリンク */
	private static final String BACK = "&laquo; 前";
	private static final String NEXT = "次 &raquo;";
	private static final String HNKK_BLNK = "&nbsp;";

	/**
	 * ページリンク部作成<br>
	 * 引数を元にページリンク部のHTMLタグを生成します。<br>
	 * なお、当メソッド内でVeloctiyToolのgetUrlを使用しています。<br>
	 * <p>
	 * ページリンクの数:sinaburocast.properties#PAGELINK_LIMIT
	 * </p>
	 * @param currentPageNo  カレントページNo
	 * @param totalPageCount 総ページ数
	 * @param param          パラメータ(不要な場合はnull)
	 * @param execMode       実行モード
	 * 						  (0:契約事業者検索一覧 1:契約事業者自動入稿一覧 2:エンドユーザ検索一覧
	 * 						  3:サーバグループマスタ一覧 4:サーバマスタ一覧 5:ワーカーマスタ一覧
	 * 						  9:サービス・サーバグループ関連一覧 10:配信プランマスタ一覧
	 * 						   )
	 * @param request        HTTPリクエスト
	 * @return               HTMLタグ文字列
	 */
    public static String makePagingLink(
    		int currentPageNo,
    		final int totalPageCount,
    		String linkUrl,
    		String param,
    		String urlPath/*,

    		final int execMode,
    		final HttpServletRequest request*/) {
							//    	final HttpSession session = request.getSession(false);
							//    	if (session == null)	return	"";

        //パラメータ補正
        if (StringUtil.isBlank(param)) {
        	param="";
        }
        if (currentPageNo < 1) {
        	currentPageNo = 1;
        } else if (currentPageNo > totalPageCount ) {
        	currentPageNo = totalPageCount;
        }

        //ページリンク数
        int pageLinkMax = PropertyUtil.getInt("PAGELINK_LIMIT");//プロパティから取得
        if (pageLinkMax == 0) {
        	return "";
        }

        		//String linkUrl = null; // TODO
		final String paramSeparator = linkUrl.contains("?") ? "&" : "?";
        StringBuilder sb = new StringBuilder();

        //String urlPath = "213"; // TODO
		//カレントページ番号が1より大きい場合、「前へ」を付与
        if (1 < currentPageNo) {
    			if (StringUtil.isNotEmpty(param)) {
					sb.append(String.format(HTML_FORMAT_HERF, urlPath + paramSeparator + param+(currentPageNo-1), BACK));
    			} else {
    				sb.append(String.format(HTML_FORMAT_HERF, urlPath+(currentPageNo-1), BACK));
    			}
        		sb.append(HNKK_BLNK);
        }

        //ページリンクの数を設定
        int count = 0;
        if (pageLinkMax > totalPageCount) {
        	count = totalPageCount;
        } else {
        	count = pageLinkMax;
        }

        //センターのページ番号を取得
        int center = (count / 2) + 1;
        //センターからラストまでの数
        int centerToEnd = count - center;
        int endPageNo = currentPageNo + centerToEnd;

        //最終ページ補正
        if (endPageNo > totalPageCount) {
        	endPageNo = totalPageCount;
        }

        int startPageNo = endPageNo - count + 1;

        //スタートページマイナス補正
        if (startPageNo < 1) {
        	endPageNo = endPageNo + Math.abs(startPageNo) + 1;
        	startPageNo = 1;
        }

        for(int i=startPageNo; i<=endPageNo; i++){
        	if( i==currentPageNo ) {
        		sb.append(String.format(HTML_CURRENT_PAGE_FORMAT, currentPageNo));
            } else if (i <= endPageNo){
        			if (StringUtil.isNotEmpty(param)) {
        				sb.append(String.format(HTML_FORMAT_HERF, urlPath + paramSeparator + param+i, i));
        			} else {
        				sb.append(String.format(HTML_FORMAT_HERF, urlPath+i, i));
        			}
            }
        	sb.append(HNKK_BLNK);
        }

        //カレントページNoが最終ページでない場合、「次へ」を付与
        if (currentPageNo != totalPageCount) {
    			if (StringUtil.isNotEmpty(param)) {
    				sb.append(String.format(HTML_FORMAT_HERF, urlPath + paramSeparator + param+(currentPageNo+1), NEXT));
    			} else {
    				sb.append(String.format(HTML_FORMAT_HERF, urlPath+(currentPageNo+1), NEXT));
    			}
        }
        return "<ul class='pageNav01'>"+sb.toString()+"</ul>";
    }


    public static void loadFile(final String filePath ,final OutputStream pOutputStream)throws Exception
	{
		FileInputStream inputStream = null;

		BufferedInputStream bufferedInputStream = null;
		BufferedOutputStream bufferedOutputStream = null;

		try
		{
			inputStream = new FileInputStream(filePath);

			bufferedInputStream = new BufferedInputStream(inputStream);
			bufferedOutputStream = new BufferedOutputStream(pOutputStream);

			byte[] buffer = new byte[1024];
			int length;

			while((length = bufferedInputStream.read(buffer)) > -1)
			{
				bufferedOutputStream.write(buffer,0,length);
				bufferedOutputStream.flush();
			}
		}
		catch(FileNotFoundException e)
		{
			throw new SystemException("File not found : " + filePath, Errors.GeneralError);
		}
		finally
		{
			InputStreamUtil.close(bufferedInputStream);
			OutputStreamUtil.close(bufferedOutputStream);
		}
	}

	/**
	 * 色を追加する。
	 *
	 * @param message
	 * @return
	 *
	 */
	public static String addRedColor(String message) {
		return SinaburoUtil.isBlankOrSpace(message)? " " : "<html><font color=red>" + message + "</font></html>";
	}

	/**
	 * エラーコードからメッセージを取得する。<br>
	 *
	 * @param defaultKey
	 * @param errorCode
	 * @return
	 */
	public static String getErrorMessage(String defaultKey, String errorCode) {
	    MessageResources messages = MessageResources.getMessageResources("application");
	    String msg = messages.getMessage(getLocale(), "errors." + errorCode);
	    if (StringUtils.isEmpty(msg)) {
	    	msg = messages.getMessage(getLocale(), defaultKey);
	    }
		return msg;
	}

	/**
	 * エラーコードからメッセージを取得する。<br>
	 *
	 * msg優先、msgがない場合にerrorCodeよりメッセージ取得
	 *
	 * @param msg
	 * @param errorCode
	 * @return
	 */
	public static String getErrorMessage2(String msg, String errorCode) {
	    if (StringUtils.isEmpty(msg)) {
		    MessageResources messages = MessageResources.getMessageResources("application");
		    msg = messages.getMessage(getLocale(), "errors." + errorCode);
	    }
		return msg;
	}

	/**
	 * application_ja.propertiesからメッセージを取得する
	 *
	 * @param key
	 * @param args
	 * @return
	 */
    public static String getMessage(String key, Object... args) {
    	if (PropertyUtil.getBoolean("devMode")) {
            S2PropertyMessageResourcesFactory factory = new S2PropertyMessageResourcesFactory();
            S2PropertyMessageResources resources = new S2PropertyMessageResources(factory, "application");
            return resources.getMessage(getLocale(), key, args);
    	}
    	else {
            return MessageResourcesUtil.getMessage(getLocale(), key, args);
    	}
    }


	/**
	 * application_ja.propertiesからメッセージを取得する
	 *
	 * @param key
	 * @param args
	 * @return
	 */
    public static String getMessageABVAPI(String lang, String key, Object... args) {
    	Locale locale = new Locale(lang);
    	if (PropertyUtil.getBoolean("devMode")) {
            S2PropertyMessageResourcesFactory factory = new S2PropertyMessageResourcesFactory();
            S2PropertyMessageResources resources = new S2PropertyMessageResources(factory, "application");
            return resources.getMessage(locale, key, args);
    	}
    	else {
            return MessageResourcesUtil.getMessage(locale, key, args);
    	}
    }

	/**
	 * application_ja.propertiesからメッセージを取得する
	 *
	 * @param key
	 * @param args
	 * @return
	 */
    public static String getMessage(String key) {
    	if (PropertyUtil.getBoolean("devMode")) {
            S2PropertyMessageResourcesFactory factory = new S2PropertyMessageResourcesFactory();
            S2PropertyMessageResources resources = new S2PropertyMessageResources(factory, "application");
            return resources.getMessage(getLocale(), key);
    	}
    	else {
            return MessageResourcesUtil.getMessage(getLocale(),key);
    	}
    }

    public static Locale getLocale() {
		return Locale.JAPANESE;
	}

}