星期一, 12月 31, 2012

[jQuery UI] autocomplete 控制項

試很久才正常的自動完成控制項, 一開始用keyup的事件綁定後,向後端拿資料,再初始化自動完成控制項, 但就是會有異常的情況,包含後端包出來的JSON的客製的,別頁又可以正常work。 最後終於試了正常的方向,在這記錄一下,以免下次要用又搞很久Orz。 測試版本為jquery ui 1.9.1版
 $( "#user" ).autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: iANGEL + "manageuser/search/" + $("#user").val(),
     cache: false,
     type:"GET",
     dataType:"json",
                    success: function( data ) {
      //check response data 
      if(data.user_data.status == 200){
       response($.map( data.user_data.result, function( item ) {
        return item;
      
       }));
      }
                       
                    }
                });
            },
   select:function(event,ui){
    $.console("[AUTOCOMPLETE] select");
    //$.console(ui.item);
    $(this).val( ui.item.User_NickName).attr("data",ui.item.User_ID);
    return false;
   }
        }).data( "autocomplete" )._renderItem = function( ul, item ) {
       //$.console(item);
       
       return $( "<li>" )
        .data( "item.autocomplete", item )
        .append( "<a>" + item.User_NickName + "</a>" )
        .appendTo(ul);
  };

星期五, 12月 21, 2012

[jQuery] 頁面刷新處理 (F5鍵)

找了一些偵側頁面被觸發刷新的檢查的二個範例 第一種是補捉按鍵的keycode但是如果按瀏覽器的重新整理無法偵測
$(document).bind("keypress keydown keyup", function(e) {
 
   if(e.which === 116) {
         
         return false;
      }
      if(e.which === 82 && e.ctrlKey) {
       
      
          return false;
      }
 
 });
第二種是比較好的方法,連上一頁跟下一頁都能一起觸發
$(window).bind('beforeunload', function() {

    return "Are u sure to reload?";
});

星期四, 12月 20, 2012

[jQuery API] ajaxPrefilter

//before ajax request handler
<>
$.ajaxPrefilter(function( options, originalOptions, jqXHR ){
  $.console("[ajaxPrefilter] ajax url:" + options.url);  

 });

星期日, 12月 16, 2012

[Javascript] 日期計算的應用


常常會用到又會忘記的日期格式運算。
找了一些文章說用format("yyyy-MM-dd")可以格式化但一直爆炸XD
找到的應用會直接更新在這篇。



//取得現在的日期
var currentTime = new Date(); 
//往前推30天
currentTime.setDate(currentTime.getDate() + (-30));
//第一天跟最後一天
var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);

 
//格式化yyyy/mm/dd
function dateFormat(date){
  var d  = date.getDate();
  var day = (d < 10) ? '0' + d : d;
  var m = date.getMonth() + 1;
  var month = (m < 10) ? '0' + m : m;
  var yy = date.getYear();
  var year = (yy < 1000) ? yy + 1900 : yy;
  
  var finalDate = year + "-" + m + "-" + d;
  return finalDate;
};

//日期相差的天數
function getDiffDays(endDate){
 var date1 = new Date();
  var date2 = new Date(Date.parse(endDate.replace("-", "/")));
  var date3 = date2.getTime() - date1.getTime();//時間差的毫秒数  
  var days = Math.floor(date3/(24*3600*1000));//相差天數
  return days;
};

星期一, 12月 10, 2012

[Javascript] Detect flash version

偵測客戶端目前有沒有裝Flash的外掛。

I agree with Max Stewart. SWFObject is the way to go. I'd like to supplement his answer with a code example. This ought to to get you started:
if(swfobject.hasFlashPlayerVersion("9.0.115"))
{
    alert("You have the minimum required flash version (or newer)");
}
else
{
    alert("You do not have the minimum required flash version");
}
Replace "9.0.115" with whatever minimum flash version you need. I chose 9.0.115 as an example because that's the version that added h.264 support.
If the visitor does not have flash, it will report a flash version of "0.0.0", so if you just want to know if they have flash at all, use:
if(swfobject.hasFlashPlayerVersion("1"))
{
    alert("You have flash!");
}
else
{
    alert("You do not flash :-(");
}

星期日, 12月 09, 2012

[Javascript] setTimeout

經常會使用的setTimeout方法,做個筆記

設定Timeout,預設只會執行一次,毫秒為單位

setTimeout(function() { myFunc(); }, 4000)

重覆執行setTimeout

function myFunc(){
  setTimeout(function(){ myFunc();},4000)
}

取消setTimeout

var timerID = setTimeout(function(){ myFunc();},4000)
clearTimeout(timerID );


設定條件停止


function myFunc(){
if(flag == true){
  setTimeout(function(){ myFunc();},4000)
}
}

星期四, 11月 29, 2012

[Javascript] 檢查IE Document Mode

客戶遇到ie無法正確指定文件模式的問題。 只要在ui加上<meta http-equiv="X-UA-Compatible" content="IE=edge">,即可。 不過要避免在它之前有其他script執行(網民建議)。 不過今天在客戶那邊處理的時候,之前把content內多加了一個IE=edge,chrome=1,會導致無法正確告訴ie使用最新的文件模式檢視(content)。 以下的語法可以偵測目前用戶的文件模式。
function checkIEDocumentMode(){
 
 if (window.navigator.appName == "Microsoft Internet Explorer")
 {
    if (document.documentMode){
     USERAGENT = document.documentMode;
    } // IE8
    else // IE 5-7
    {
     USERAGENT = 5; // Assume quirks mode unless proven otherwise
       if (document.compatMode)
       {
          if (document.compatMode == "CSS1Compat")
           USERAGENT = 7; // standards mode
       }
    }
    // the USERAGENT variable now contains the document compatibility mode.
    $.console("checkIEDocumentMode:" + USERAGENT);
    //var $metaTag = $("");
    //$metaTag.appendTo("head");
    //content="IE=Value" value types:5、6、7、8、9、10、EmulateIE7、EmulateIE8、Edge
 }
}

星期五, 11月 23, 2012

[jQuery] 價錢格式化

想要讓你網站顯示的數字讓使用者更容易閱讀嗎?
可以使用jquery price format 來快速完成唷
http://jquerypriceformat.com/


星期日, 11月 18, 2012

[Java] Hashmap Race condition

最近發生了一個hashmap的嚴重bug,跟同事查了才知道這是Java hashmap常見的問題之一。
由此可見大家都是java的菜鳥XD,
發生原因在於hashmap當滿了之後,如果有多個threads同時對他put或get就會產生Race Condition。
所以可以採用Java Synchronized來處理。參考這位大大的心得:[Java] Synchronized 心得
總共有三種方法可以使用

[jQuery plugin] ajaxForm 與 validator 整合範例

時常會需要利用到jquery form與 validator 二個元件整合的應用。 把範例記錄一下 :D 範例網址:
注意在submithandler事件裡面要改用ajaxSubmit的方法而不是ajaxForm。

http://jquery.bassistance.de/validate/demo/ajaxSubmit-intergration-demo.html

jQuery(function() {
  // show a simple loading indicator
  var loader = jQuery('
loading...
') .css({position: "relative", top: "1em", left: "25em", display: "inline"}) .appendTo("body") .hide(); jQuery().ajaxStart(function() { loader.show(); }).ajaxStop(function() { loader.hide(); }).ajaxError(function(a, b, e) { throw e; }); var v = jQuery("#form").validate({ submitHandler: function(form) { jQuery(form).ajaxSubmit({ target: "#result" }); } }); jQuery("#reset").click(function() { v.resetForm(); }); });

星期二, 11月 13, 2012

[Alfresco] User Quota Issue

如何讓alfresco的quota即時更新, alfresco的工程師說可透過Java backend的ContentUsageService.getUserUsage(userName) 或 覆寫userUsageCollapseJob
https://forums.alfresco.com/en/viewtopic.php?f=27&t=43431
You should be able to get the up-to-date user usage via ContentUsageService.getUserUsage(userName) or else you could consider overriding the userUsageCollapseJob to run more often (eg. every minute instead of every 5 minutes).

If you're running Enterprise 3.4.6 and need more help with this issue then please contact Alfresco Support (if you haven't already).

Regards,
Jan

Ref:
Interface ContentUsageService

星期一, 11月 12, 2012

[Java] GZIP Filter 與 404 錯誤頁面的臭蟲

起因:
GZIPFilter壓縮遇到不存在的網頁會爆炸,如下所示:


這個網頁無法使用

http://serverip/test.jspddd 的網頁可能暫時無法使用或被永久移至新網址。
錯誤 330 (net::ERR_CONTENT_DECODING_FAILED): 未知的錯誤。


2012/11/13
目前尚未找到解法,不過透過HttpServletResponseWrapper目前竟然無法成功拿到各網頁的content,唯有在最後一層的Filter呼叫啊HttpServletResponseWrapper才有辦法拿到



Reference:
What is the simplest way to display httpServletResponse.sendError(403, “My Message”) status from JSTL
How to make a filter to detect if the user requested a not found page?

[Hadoop] WordCount Sample


記錄一下第一次跑Hadoop WordCount Job的過程 :)

1. 建立HDFS資料夾
#全部的資料夾會自動建立
hduser@hadoop-master:/usr/local/hadoop$hadoop dfs -mkdir /home/hduser/wordcount

2. 匯入要分析的文件資料(local-dir)到HDFS資料夾

$hadoop dfs -copyFromLocal
#匯入
hduser@hadoop-master:/usr/local/hadoop$hadoop dfs -copyFromLocal /home/hduser/wordcount /home/hduser/wordcount

#查看匯入的資料
hduser@hadoop-master:/usr/local/hadoop$ hadoop dfs -ls /home/hduser/wordcount
Warning: $HADOOP_HOME is deprecated.

[Hadoop] WordCount

在寫Hadoop job的時候,如果你有分package的話,
匯出.jar的時候請記得選取Main Class進入點,如下所示

然後再執行job的時候就不會說找不到了
hadoop@client:~/wordcount$ hadoop jar exercise.wordco.jar WordCo /user/hadoop/wordcount/pg5000.txt /user/hadoop/wordcount/output



星期五, 11月 09, 2012

Chrome: Uncaught SyntaxError: Unexpected end of input


Chrome: Uncaught SyntaxError: Unexpected end of input

今天遇到一個找很久的BUG,
本來以為是JS裡面的{}沒對好,結果是網頁上的超連結的javascript:void(0)設錯了==
少打了void後面的(0)
這個手殘的錯誤似乎出現好幾次了XD


[jQuery plugin] 如何reset from

jquery validator常常submit完要重新清除,常用到的語法。
在此記錄一下。

$(form)[0].reset();

[jQuery plugin] validator fileupload


記錄如何驗證上傳元素,只要加上accept,filesize即可。

 fileToUpload:{ 
accept: "png|jpe?g|gif",
filesize: 1048576
 }

[jQuery plugin] Validator 如何在選擇元素select驗證

常常會用到的validator於選擇元素上的驗證,原來也是加個required就好,記一下 XD

$("#bankForm").validate({
   rules:{
    
     citys:{
     required:true
     }
                     
});
最後發現請選擇的option的value要記得設"",不然會無法動作。

                               
                           

星期六, 11月 03, 2012

[Javascript] 檢查json的key是否存在



太常用到了,記錄一下。使用hasOwnProperty就可以輕鬆解決嚕 wow

You don't need jQuery for this, just JavaScript. You can do it a few ways:
  • typeof d.failed- returns the type ('undefined', 'Number', etc)
  • d.hasOwnProperty('failed')- just in case it's inherited
  • 'failed' in d- check if it was ever set (even to undefined)

星期四, 11月 01, 2012

[Alfresco] Upload Avatar Javascript API


//js

/**
 * User Profile Avatar Upload method
 *
 * @method POST
 * @param username {string}
 *        filedata {file}
 */

function main()
{
   try
   {
      var filename = null;
      var content = null;
      var username = null;

      // locate file attributes
      for each (field in formdata.fields)
      {
         if (field.name == "filedata" && field.isFile)
         {
            filename = field.filename;
            content = field.content;
         }
         else if (field.name == "username")
         {
            username = field.value;
         }
      }

      // ensure all mandatory attributes have been located
      if (filename == undefined || content == undefined)
      {
         status.code = 400;
         status.message = "Uploaded file cannot be located in request";
         status.redirect = true;
         return;
      }
      if (username == null || username.length == 0)
      {
         status.code = 500;
         status.message = "Username parameter not supplied.";
         status.redirect = true;
         return;
      }

      var user = people.getPerson(username);
      // ensure we found a valid user and that it is the current user or we are an admin
      if (user == null ||
          (people.isAdmin(person) == false && user.properties.userName != person.properties.userName))
      {
         status.code = 500;
         status.message = "Failed to locate user to modify or permission denied.";
         status.redirect = true;
         return;
      }

      // ensure cm:person has 'cm:preferences' aspect applied - as we want to add the avatar as
      // the child node of the 'cm:preferenceImage' association
      if (!user.hasAspect("cm:preferences"))
      {
         user.addAspect("cm:preferences");
      }

      // remove old image child node if we already have one
      var assocs = user.childAssocs["cm:preferenceImage"];
      if (assocs != null && assocs.length == 1)
      {
         assocs[0].remove();
      }

      // create the new image node
      var image = user.createNode(filename, "cm:content", "cm:preferenceImage");
      image.properties.content.write(content);
      image.properties.content.guessMimetype(filename);
      image.properties.content.encoding = "UTF-8";
      image.save();

      // wire up 'cm:avatar' target association - backward compatible with JSF web-client avatar
      assocs = user.associations["cm:avatar"];
      if (assocs != null && assocs.length == 1)
      {
         user.removeAssociation(assocs[0], "cm:avatar");
      }
      user.createAssociation(image, "cm:avatar");

      // save ref to be returned
      model.image = image;
   }
   catch (e)
   {
      var x = e;
      status.code = 500;
      status.message = "Unexpected error occured during upload of new content.";
      if(x.message && x.message.indexOf("org.alfresco.service.cmr.usage.ContentQuotaException") == 0)
      {
         status.code = 413;
         status.message = x.message;
      }
      status.redirect = true;
      return;
   }
}

main();



//description

  Avatar Upload
  Upload avatar file content and apply to person preferences
 
  user
  required
  /slingshot/profile/uploadavatar



//template
{
<#if person.childAssocs["cm:preferenceImage"]??>
"avatar": "${person.childAssocs["cm:preferenceImage"][0].downloadUrl}"
</#if>
<#if person.associations["cm:avatar"]??>
<#if person.associations["cm:avatar"][0].childAssocs["rn:rendition"]??>
,"avatarthumb": "${person.associations["cm:avatar"][0].childAssocs["rn:rendition"][0].downloadUrl}"
</#if>
</#if>
}

星期三, 10月 24, 2012

[Java] 取得Image的byte[]

今天廠商在哭爸圖片解析度太低,只好先把JPEG壓縮拿掉。
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(encoderOutputStream);
encoder.encode(bufferedResizedImage);
查了一下如何把BufferedImage轉成Byte[]存在db,或你其他的需求。
BufferedImage originalImage = ImageIO.read(new File("c:\\image\\mypic.jpg"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( originalImage, "jpg", baos );
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
這篇文章好心整理了java中的幾個處理方法,有空可以看看。
High-Quality Image Resize with Java

星期日, 10月 21, 2012

[Javascript] Callback function

Callback在寫oop的javascript中非常好用, 找到了一篇非常容易理解的好文章。
Callback Functions in JavaScript
function mySandwich(param1, param2, callback) {
    alert('Started eating my sandwich.\n\nIt has: ' + param1 + ', ' + param2);
    if (callback && typeof(callback) === "function") {
        callback();
    }
}

mySandwich('ham', 'cheese', 'vegetables');

星期六, 10月 20, 2012

[jQuery] ajax 異常錯誤

昨天遇到一個很奇怪的AJAX呼叫錯誤, 最後發現觸發的元素內的javascript:void(0)打錯了,導致JS異常=.= 找了一陣子才找到,真是扯XD

星期二, 10月 16, 2012

[JSON] 取得json keys 列表

原本都用陣列來儲存列表,這次遇到用keys來表示原本陣列的數量<
for (var key in repaymentlist) 
  {
   if (repaymentlist.hasOwnProperty(key))
   {
    $.console(repaymentlist[key]);
    
   }
  }

星期四, 10月 11, 2012

[Javascript] 取得物件的類別名稱

如果取得你自訂類別的名稱

function getClassName(obj) {
if (typeof obj != "object" || obj === null) return false;
return /(\w+)\(/.exec(obj.constructor.toString())[1];
}

星期三, 9月 26, 2012

[jQuery] ie8 元素id與js變數產生衝突

今天程式發生了一個IE8 bug,因使用js來做前端的i18n,但同事有把元素的id取得跟i18n js的變數一樣,導致js錯誤發生。

 HTML
<span id=”stupid_ie_8”></span>

//i18n key
stup_ie_8 = “IE好弱”;

//script
$(“#stupid_ie_8”).html(stup_ie_8);

星期二, 9月 25, 2012

[jQuery] append function error in IE8

今天被fire一個bug,有關於append元素的時候,畫面沒有被顯示。
主要是append的元素結構沒有正常的結尾,ie8無法自動修正。

一般html頁面元素沒有正常結尾也會空白

星期三, 9月 19, 2012

[jQuery API] 拍賣倒數計時器 min seconds: 倒數十下

現在所有團購網都流行的倒數計時器方法,參考至某團購網站XD

$(function() {
    setInterval(function () {
  var m = parseInt($('#mis').text());
  //console.log("start m:" + m);
  if (isNaN(m)) {
   $('#mis').text('0');
  } else {
   m=m+10;
   //console.log("m:" + m);
   if (m>=10) { 
    m=m-1;   
    if(m>=10)
    {
     $('#mis').text(m-10);
    }
    else
    {
     $('#mis').text(m);
    }
   } 
  }
 }, 100);//setInterval
 });  

星期六, 9月 15, 2012

[jQuery plugin] jquery Validator表單驗證器 番外篇(進階一點的需求吧XD)

Validator真是好物呀,先前有整理一個入學篇,覺得篇福愈來愈長了,
遇到的新問題就記在這篇吧!!

Q.如何指定要特地驗證某一個欄位
有時候你只想要透過別的動作來檢查某個欄位,可以使用valid方法,回傳值為1:驗證通過,0:驗證失敗

var validateForMoney = $("#borrowMoney").valid()//1:ok,0:false
$.console("validateForMoney:" + validateForMoney);
var validateForRate = $("#borrowRate").valid();
$.console("validateForRate:" + validateForRate);

Q.取得全部驗證失敗的欄位有哪些?
//透過numberOfInvalids方法就會回傳數字了,$validatedPostBorrow是我驗證器的變數
$.console("invalid fields:" + $validatedPostBorrow.numberOfInvalids());


Reference:
jQuery Validate Plugin - Trigger validation of single field

星期二, 9月 04, 2012

[jQuery plugin] 來個倒數計時器吧

倒數計時器能增加使用者有感的提示XD常用到拍賣商品上的特效。
找了幾個現成的元件
請參考以下這篇網誌的20以上的jQuery countdown plugins (有擷圖)
http://www.tripwiremagazine.com/2012/05/jquery-countdown-scripts.html

星期一, 9月 03, 2012

[jQuery API] 實作Facebook的捲軸拉到底更新資訊 Facebook like scroll bar detection

如何使用jQuery偵測捲軸的位置!! 實作Facebook like的更新狀態列記錄
$(function () {
             var $win = $(window);

             $win.scroll(function () {
                 if ($win.scrollTop() == 0)
                     alert('Scrolled to Page Top');
                 else if ($win.height() + $win.scrollTop()
                                == $(document).height()) {
                     alert('Scrolled to Page Bottom');
                 }
             });
         });


Here's some explanation:

$(window).height() - returns height of browser viewport

$(document).height() - returns height of HTML document

$(window).scrollTop() – returns the current vertical position of the scroll bar.

So if the scroll bar is at the very top, the value of $(window).scrollTop() will be Zero. Similarly if the value of the browser viewport + vertical position of scroll bar = document height, then the user has scrolled to the bottom of the page.

寫成外掛的方式來看看!!

星期四, 8月 30, 2012

[Javascript] Closure Compiler

在使用yui compressor時遇到編譯失敗的問題,可透過Google closure compiler:http://closure-compiler.appspot.com/home找到檔案為何無法正確編譯(但js與css能正常於網站執行),錯誤的問題不外乎是語法不嚴僅,如使用到保留字等等。


[Regex] 找出不包含特定字的結果 don't contain specified word


http://www.anydotcom.com/test/search.cfm?metric=blah&selector=size&value=1
http://www.anydotcom.com/test/search.cfm?metric=blah2&selector=style&value=1
http://www.anydotcom.com/test/search.cfm?metric=blah3&selector=size&value=1
http://www.anydotcom.com/test/details.cfm?metric=blah&selector=size&value=1

找出不包含details.cfm這個字的
(^((?!details.cfm).)*$)

Reference:

星期日, 8月 05, 2012

[Java] Jersey @DefaultValue

有時候你的參數是選項輸入的,即可使用 @DefaultValue
public String getOwnerQuoteList(
   
   @PathParam(value="OWP_ID") String OWP_ID,
   @DefaultValue("1") @QueryParam(value="page") int pageIndex,
   @DefaultValue("10") @QueryParam(value="size") int pageSize,
   @DefaultValue("dateline")@QueryParam(value="sort") String sort,
   @DefaultValue("false") @QueryParam(value="asc") boolean asc) throws JSONException{

}

星期六, 8月 04, 2012

[jQuery] $.ajax 讀取影像

似乎不需要弄$.ajax也可以達到這個需求XD

var img = $("#imgElem").attr('src', 'http://somedomain.com/image.jpg')
                      .load(function() {
                         if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
                             alert('broken image!');
                         } else {
                             $("#something").append(img);
                         }
                      });

Reference:

星期三, 8月 01, 2012

[Java] 快快樂樂使用 Jersey MVC 架構

先前利用jersey都只有在controller端直接將JSON物件轉成字串輸出,
jersey也提供MVC的方式來支援JSP來做template(View)
讓畫ui結構更直覺多了。

星期二, 7月 31, 2012

[jQuery plugin] Pagination

分頁元件產生 :),採用的為以下這個jquery pagination
http://www.d-scribe.de/webtools/jquery-pagination/demo/demo_options.htm

範例:
$("#pageContainer").pagination(
      total,
      {
       items_per_page:PAGESIZE,
       num_display_entries:10,
       num_edge_entries:2,
       link_to:"javascript:void(0)",
       prev_text:"上一頁",
       next_text:"下一頁",
       callback:function(currentPage,container){
});

其他的分頁
http://blog.ajaxmasters.com/jquery-pagination-plugin/ 
http://tympanus.net/codrops/2009/11/17/jpaginate-a-fancy-jquery-pagination-plugin/

[Javascript] Validate E-mail

驗證信箱格式,讚的次數蠻高的,參考至這篇討論 Validate email address in Javascript?
function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\
".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA
-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
} 
其他驗證語法 ^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$

星期一, 7月 30, 2012

[Jersey] Client Post

Form post

MultivaluedMap formData = new MultivaluedMapImpl();
formData.add("name1", "val1");
formData.add("name2", "val2");
ClientResponse response = webResource.type("application/x-www-form-urlencoded").post(ClientResponse.class, formData);

Post JSON
需要注意在post json要這樣使用webResource.type(MediaType.APPLICATION_JSON).post(), 否則會回傳 415 Unsupported media type
public String isExist(
   String parentUUID,
   String objName,
   String alf_ticket){
  
  String response = null;
  
  String apiURL = this.getEndpoint() + "/repo/object/check?ticket=" + alf_ticket;
  
  this.client = Client.create();
  
  //input body
  JSONObject jsonBody = new JSONObject();
  try {
   jsonBody.put("parentUUID", parentUUID);
   jsonBody.put("objName", objName);
  } catch (JSONException e) {
   System.out.println("[RepositoryObject] Fail to create json");
  }
  
  //create resource
  this.webResource = this.client.resource(apiURL);
//  this.webResource.type("application/json");
  
  //do post
  this.clientResp =  webResource.type(MediaType.APPLICATION_JSON).post(
    ClientResponse.class,jsonBody.toString());
  
  //get string of response
  response = this.clientResp.getEntity(String.class);
  
  System.out.println("[RepositoryObject] status:" + this.clientResp.getStatus());
  System.out.println("[RepositoryObject] response:" + response);
  
  return response;
 }

星期四, 7月 19, 2012

[jQuery plugin] $.address 快速連按上下頁按鈕

最近實作記錄讓ajax執行後能記錄行為到瀏覽器的上下頁按鈕, 因而選擇了$.address plugin。 原本運作上蠻正常的,不過在Firefox上面快速的連按上或下一頁按鈕時,會造成整個ui畫出來不對的現在。 因為firefox ajax request順序時間差不一致造成,因此在URL Change Event加入Queue即可解決。

 $.address.externalChange(function(event){
//  $.console("[address externalChange] event value:" + event.value);
  var controller = event.pathNames[0];
    var parameter = event.pathNames[1];
    if(!isNullOrUndefiend(controller)){
//     $.console("[address externalChange] Trigger currentFolder:" + currentFolder);
     //2012/07/19
     //When click back button quickly on firefox, 
     //must use queue to avoid duplicated data binding  
     $(this).queue(function(){
      addressEventHandler(controller,parameter);
      $(this).dequeue();
     });
    }else{
     $.console("[address externalChange] Controller not found");
    }
 }); 

Tip:有時候外掛會無法取得externalChange event.value的值,目前還沒找出正確原因。目前直接用變更外部連結的方法處理。
window.location.href = "yourpage.jsp#ControllerName/ + "Parameters";

星期三, 7月 18, 2012

[jQuery API] 指定多重條件的selector (Multiple Attribute Selector)

有時候需要透過多個條件濾掉不需要的元素,
順便記錄一下,方便好查:)
[]區開就好,以下範例是指綁定No equal的


$("#manage_title").find("div[class!=manage_avatar][class!=manage_action]").click(function(){

});


Reference:
http://api.jquery.com/multiple-attribute-selector/

星期日, 7月 15, 2012

[jQuery api] Stop ajax

如何停止執行中的ajax的request,在執行ajax呼叫後,會回傳一個instance,再透過abort即可停用。


var request = $.ajax({
    type: 'POST',
    url: 'someurl',
    success: function(result){}
});
然後再把request abort即可
request.abort();

[jQuery plugin] Validator 驗證多個Radio跟checkbox範例

今天遇到要檢查多個checkbox至少要選一下就順便記錄一下。
只要將你的checkbox內的name屬性設為 <你取的名字>[]
如下範例 candidate_usr[],記得要加上陣列的符號[]

 $("#candidateForm").validate({
    rules:{
     'candidate_usr[]':{
      required:true
     }
    },
    messages:{
     'candidate_usr[]':{
      required: "請至少選擇一名委託者"
     }
    },
    submitHandler: function(form) {
     //get candicates 
     var $checkboxes 
      = $("#candidateForm").find("input:checkbox:checked").each(
       function(i,cbObj){
        var selectedExpertID = $(cbObj).val();
        $.console("selected id:" + selectedExpertID);
       });
    
    }
  });

Reference:
http://jquery.bassistance.de/validate/demo/radio-checkbox-select-demo.html

星期一, 7月 02, 2012

[Alfresco] 實作alfreso javabacked 上傳心得 How to upload file using java backed web script

本文記錄使用Java API實作上傳的測試心得
上網找到的取得HttpServletRequest 都不適用於alfresco 3.4.5版本

// NOTE: This web script must be executed in a HTTP Servlet environment
//        if (!(req instanceof WebScriptServletRequest)) {
//                throw new WebScriptException(
//                                "Content retrieval must be executed in HTTP Servlet environment");
//        }
     
// HttpServletRequest httpReq = ((WebScriptServletRequest)req).getHttpServletRequest();

發生錯誤如下: ClassCastException


protected Map<string, object> executeImpl(
   WebScriptRequest req,
   Status status, 
   Cache cache) {
WrappingWebScriptRequest wrappingWebScriptRequest = (WrappingWebScriptRequest) req;
  WebScriptRequest webScriptRequest = wrappingWebScriptRequest.getNext(); 
  WebScriptServletRequest servletRequest = (WebScriptServletRequest) webScriptRequest;
  FormField uploadFile = servletRequest.getFileField("file");
  //file field     
  uploadFileName = uploadFile.getFilename();
     uploadMIMEType = uploadFile.getMimetype();
     uploadContent = uploadFile.getInputStream();
     System.out.println("[form data] filename:" + uploadFileName);
     System.out.println("[form data] mimetype:" + uploadMIMEType);
//do something
}

用以下這段程式就能正常取得上傳檔案了,先前的中文問題出在client指定錯誤編碼了,繞了一大圈竟然是手誤呀!!
  

//how to get WebScriptServletRequest
  WrappingWebScriptRequest wrappingWebScriptRequest = (WrappingWebScriptRequest) req;
  WebScriptRequest webScriptRequest = wrappingWebScriptRequest.getNext(); 
  WebScriptServletRequest servletRequest = (WebScriptServletRequest) webScriptRequest;
  
  //get data form form
  FormData formData = (FormData)servletRequest.parseContent();
  FormData.FormField[] formFields = formData.getFields();
  int fieldsLen = formFields.length;
  for(int i=0;i

如果要做更複雜的行為不在這篇的討論範例 :)

星期三, 6月 27, 2012

[Alfresco ] How to update content with ContentWriter class

此問題欲解決要利用ContentWriter變更Node的內容, 不是指新增版本的方式,目前尚未用到,待驗證 :)
ContentWriter contentWriter = contentService.getWriter(nodeCurrentLogFile, ContentModel.PROP_CONTENT, true);
contentWriter.setMimetype("text/plain");
FileChannel fileChannel = contentWriter.getFileChannel(false);
ByteBuffer bf = ByteBuffer.wrap(logLine.getBytes());
fileChannel.position(contentWriter.getSize());
fileChannel.write(bf);
fileChannel.force(false);
fileChannel.close();

星期一, 6月 25, 2012

[Alfresco] Java Foundation API

記錄一下Alfresco low level的java API學習資源 :)


Content repository services

Content repository services are the fundamental services for working with content in Alfresco. Content repository services are written in Java.
Content repository services are built on the storage and query engines. As with the engines, the same infrastructure is shared. The concept of users and groups is introduced into these services, such as recording the author of content, who has content locked or access to content. Implementation of the standards-defined services is packaged into the Alfresco content repository.
Out-of-the-box content repository services provide capabilities to:
  • Organize and manage content
  • Control versions
  • Record changes and updates
  • Enforce security
  • Model content types
  • Search for information in the repository



Search Service
http://wiki.alfresco.com/wiki/Search#Search_Parameters
https://wiki.alfresco.com/wiki/Search_Documentation#Lucene_Language
How to exceed Lucene Limit Search?

[Java] Jersey Client

本文記錄如何使用Jersey client API
Reference:
https://blogs.oracle.com/enterprisetechtips/entry/consuming_restful_web_services_with

星期二, 6月 19, 2012

[Alfresco] How to create Java backed web script in alfresco (3)

這個範例在Google都找的到,沒做多大的修改。
重點是記錄Spring Bean XML的差異性。
Spring Bean XML 
重點在於property標籤設定,如果你參考了foundation的api請記得把他加進來!!,
所以這個範例要使用了Repository,請設定rel=repositoryHelper。
 <bean
         id="webscript.org.example.simplejavadir.get"
         class="org.example.SimpleJavaDir"
         parent="webscript">
                <property name="repository" ref="repositoryHelper"/>
        </bean>

Java backed controller
透過xml的設定, 就可以透過 public void setRepository(Repository repository) {}直接呼叫alfresco的核心api了。
package org.example;
import java.util.HashMap;
import java.util.Map;

import org.alfresco.repo.model.Repository;
import org.alfresco.service.cmr.repository.NodeRef;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;

public class SimpleJavaDir extends DeclarativeWebScript {
 
 private Repository repository;
 
    private static Log s_logger = LogFactory.getLog(SimpleJavaDir.class);


 public void setRepository(Repository repository) {
  this.repository = repository;
 }

 @Override
 protected Map<string, object>  executeImpl(WebScriptRequest req,
   Status status, Cache cache) {

  s_logger.debug("SimpleJavaDir entry");
  
  // extract folder listing arguments from URI
  String verboseArg = req.getParameter("verbose");
  Boolean verbose = Boolean.parseBoolean(verboseArg);
  Map<string, string>  templateArgs = req.getServiceMatch().getTemplateVars();
  String folderPath = templateArgs.get("folderpath");
  String nodePath = "workspace/SpacesStore/" + folderPath;

//  @param1 one of "node", "path", or "avmpath"
  NodeRef folder = repository.findNodeRef("path", nodePath.split("/"));
  // validate that folder has been found
  if (folder == null) {  
   throw new WebScriptException(Status.STATUS_NOT_FOUND, "Folder "
     + folderPath + " not found");
   //you will see the followning messages in your server
//    ERROR [extensions.webscripts.AbstractRuntime] Exception from executeScript - redirecting to status template error: 05190001 Folder Company Homes not found

  }
  
  // construct model for response template to render
   Map<string, object> model = new HashMap<string, object>();
  model.put("verbose", verbose);
  model.put("folder", folder);
  model.put("nodePath", nodePath);
  return model;
 }
}


星期日, 6月 10, 2012

[Java] Zxing create a QRCode

http://www.cnblogs.com/tompandas/archive/2012/04/10/2440513.html
http://code.google.com/p/zxing/downloads/list

[phpmyadmin] stored procedure and trigger

查看目前已建立的stored procedure
show procedure status

查看目前已建立的stored procedure


select * from information_schema.routines where routine_type = 'procedure'


查看指定資料庫內已有的stored procedure


select * from information_schema.routines where routine_type = 'procedure' and routine_schema = 'your_db'

新建stored procedure


  1. Open phpMyadmin.
  2. Select a database to work with.
  3. Open the SQL tab.
  4. Select all of the SQL statements between the DELIMITER statements in your stored procedure script. Do not include the DELIMITER statements! Here’s what my example script should look like:
    DROP PROCEDURE IF EXISTS spFoo $$
    CREATE PROCEDURE spFoo ()
    BEGIN
     SELECT 'Foo' FROM DUAL;
    END $$
    
  5. In the delimiter field, just below the SQL editor’s text area, enter $$ as your delimiter.

Reference:
如何使用MySQL的Stored Procedure與Trigger
Using phpMyAdmin to Create Stored Procedures

星期日, 6月 03, 2012

[Java] Jersey Session

Session控制
@Path("/helloworld")
public class HelloWorld {

    @GET
    @Produces("text/plain")
    public String hello(@Context HttpServletRequest req) {

        HttpSession session= req.getSession(true);
        Object foo = session.getAttribute("foo");
        if (foo!=null) {
                System.out.println(foo.toString());
        } else {
                foo = "bar";
                session.setAttribute("foo", "bar");
        }
        return foo.toString();


    }
}

[Java] java.lang.UnsupportedClassVersionError: example/Hello : Unsupported major.minor version 51.0 (unable to load class example.Hello)

今天在local編譯完java後,丟到機器後就爆炸了。
是JDK版本不一樣導致XD,記錄一下這個痛!!

java.lang.UnsupportedClassVersionError: example/Hello : Unsupported major.minor version 51.0 (unable to load class example.Hello)

Major Class Versions of Various JDK
Following are the major version of class file format in standard JDK environment.

JDK 1.1 = 45
JDK 1.2 = 46
JDK 1.3 = 47
JDK 1.4 = 48
JDK 1.5 = 49
JDK 1.6 = 50

[Java] org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8005]]

案例:
本地端(mac)的JRE使用1.7的64bit版本,build出來的程式在機器上都爆炸XD
目前先把elicpse的環境設回1.6的版本就可以在server正常跑了。
有遇到一樣情形的朋友一定要先確定Java的版本到底有沒有一致

專案按右鍵選擇Properties設定
JAVA BUILD PATH 設定


JAVA COMPILER 設定

[Java] Name comp is not bound in this Context

星期五, 6月 01, 2012

[Mysql] Host ‘yourhost’ is not allowed to connect to this MySQL server錯誤

連線MySQL時,出現標題上的錯誤訊息,
這個問題是此IP位址未授權給MYSQL
解決方法:直接在下SQL語法設定即可

補充:
進入設定檔 改bind-address

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0

1.登入mysql
shell>mysql -u root -p
mysql>use mysql

2.設定資料庫的使用權限

允許所有連線

mysql>GRANT ALL PRIVILEGES ON *.* TO ‘MySQL帳號’@'%’ IDENTIFIED BY ‘MySQL密碼’ WITH GRANT OPTION;
Tip:% 代表不限client的連線ip

設定特地連結的ip

mysql>GRANT ALL PRIVILEGES ON *.* TO ‘MySQL帳號’@'你要連線IP‘ IDENTIFIED BY ‘MySQL密碼’ WITH GRANT OPTION;

3.最後一定要更新權限
mysql>FLUSH PRIVILEGES;

Reference:
[MySQL]解決ERROR 1130: Host ‘yourhost’ is not allowed to connect to this MySQL server錯誤無法遠端連接MySQL:message from server: "Host xxx is not allowed to connect to this MySQL server"
MySQL的重要語法

[Java] DBCP Sample

簡單的DBCP範例,主要記錄Reference物件的用法
Setting up initial context for DBCP connection pool.

[Java] OpenID4Java

今天在處理OpenID的議題時,發現在local測試跑很正常。
但BUILD到伺服器的時候,會產生類別找不到的問題。
發現在tomcat/lib放了二個版本的openid4java的函式庫,
把舊的砍掉就執行正常了

星期四, 5月 31, 2012

[Java] Cannot create JDBC driver of class '' for connect URL 'null'

想要透過local的專案直接連mysql所遇到的
Cannot create JDBC driver of class '' for connect URL 'null'錯誤訊息的方法,
Google到的資訊實在太多做法了,把試成功的經驗記錄一下。

step1:在專案內的WEB-INF/加入context.xml檔

<xml version="1.0" encoding="UTF-8"?>
<Context >
<Resource
                name="jdbc/資料表連接名稱"
                auth="Container"
                type="javax.sql.DataSource"
        maxActive="100"
                maxIdle="30"
                maxWait="10000"
        username="root"
                password="密碼"
                driverClassName="com.mysql.jdbc.Driver"
        autoReconnect="true"
                removeAbandoned="true"
                removeAbandonedTimeout="300"
                logAbandoned="false"
        url="jdbc:mysql://遠端IP:3306/資料庫名稱"/>
        
Context>


Tip:DBCP已經設全域設定了,所以這個不用加
step2:在專案的web.xml加入

<resource-ref>
      <description>DB Connectiondescription>
      <res-ref-name>jdbc/資料表連接名稱res-ref-name>
      <res-type>javax.sql.DataSourceres-type>
resource-ref>

非常重要!!記得要重新開Tomcat!!

星期三, 5月 30, 2012

Mac lion Eclipse 設定 JDK7

在lion安裝完JDK7後,它的路徑已經先之前安裝的路徑不一樣了。
目前預設的安裝目錄是在
/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home

之後打開Eclipse後,透過偏好設定的JAVA中的Installed JREs,指定新的JDK就可以了。


找到的參考資料
If you are a Java developer who wants to do Java development on Mac OS X 10.7, here are a few not-so-tricky steps to follow to get going:
  1. Go to http://connect.apple.com and sign in with your Apple ID (developer account may be required – it’s free).
  2. From the list titled “Downloads for Apple Developers”, select the item labeled “Java for Mac OS X 10.7 Update 1 Developer Package” (release date Nov 8, 2011) then download and install the package.
  3. The JDK will be installed into a different location then previous. This will result in IDEs (such as Eclipse) being unable to locate source code and java docs.
  4. At the time of writing the JDK ended up here:
    /Library/Java/JavaVirtualMachines/1.6.0_26-b03-383.jdk/Contents/Home
  5. Open up Eclipse preferences and go to Java > Installed JREs page
  6. Rather than use the “JVM Contents (MacOS X Default) we will need to use the JDK location
  7. At the time of writing Search is not aware of the new JDK location; we we will need to click on the Add button
  8. From the Add JRE wizard choose “MacOS X VM” for the JRE Type
  9. For the JRE Definition Page we need to fill in the following:
    JRE Home: /Library/Java/JavaVirtualMachines/1.6.0_26-b03-383.jdk/Contents/Home
  10. The other fields will now auto fill, with the default JRE name being “Home”. You can quickly correct this to something more meaningful such as:
    JRE name: System JDK
  11. Finish the wizard and return to the Installed JREs page
  12. Choose “System JDK” from the list
  13. You can now develop normally with:

[Java] Filter 變更 jsp輸出內容

今天為了讓jsp上面的js檔案能使用SVN的版號做版本控制,
又為了讓同事能不用變更之前放在jsp裡面的script path,
所以想到了可以透過filter來將jsp輸出前把script path加上SVN的版號,
這樣就可以模擬出使用者每次都能讀到最新變更的Javascript

以下是簡單置換內容的Filter Sample

星期二, 5月 29, 2012

星期一, 5月 28, 2012

[Alfresco] Alfresco Namespace

Note: This list will expand / change between now and the next release.
NamespaceCommon PrefixDescription
http://www.alfresco.orgalfGeneral Alfresco Namespace
http://www.alfresco.org/model/dictionary/1.0dData Dictionary model
http://www.alfresco.org/model/system/1.0sysRepository system model
http://www.alfresco.org/model/content/1.0cmContent Domain model
http://www.alfresco.org/model/application/1.0appApplication model
http://www.alfresco.org/model/bpm/1.0bpmBusiness Process Model
http://www.alfresco.org/model/site/1.0stSite Model
http://www.alfresco.org/model/forum/1.0fmForum Model
http://www.alfresco.org/model/user/1.0usrUser model (in repository.jar)
http://www.alfresco.org/view/repository/1.0viewAlfresco Import / Export View
http://www.alfresco.org/model/action/1.0actAction service model
http://www.alfresco.org/model/rule/1.0ruleRule service model
http://www.alfresco.org/ws/service/authentication/1.0authAuthentication Web Service
http://www.alfresco.org/ws/service/repository/1.0repRepository Web Service
http://www.alfresco.org/ws/service/content/1.0contentContent Web Service
http://www.alfresco.org/ws/service/authoring/1.0authorAuthoring Web Service
http://www.alfresco.org/ws/service/classification/1.0clsClassification Web Service
http://www.alfresco.org/ws/cml/1.0cmlContent Manipulation Language
http://www.alfresco.org/ws/model/content/1.0cmWeb Service Content Domain Model
http://www.alfresco.org/ws/headers/1.0hdrSOAP Headers
http://www.alfresco.org/model/workflow/1.0wfWorkflow Model(link is to Alfresco simple workflow model, not generally extended)

[Eclipse] 設定JRE與Tomcat runtime

簡單記錄一下Eclipse設定JRE與Server Runtime擷圖XD

JRE Runtime
Step1: Windows-> Preference
Step2: Java-> Installed JREs


Server Runtime

Step1: Windows-> Preference
Step2: Server-> Runtime Environments
Step3: Add, 新增一個runtime server

Step4: 選擇新的runtime enviornment,範例為tomcat 7.0

Step5: 設定Tomcat安裝目錄
C:\Program Files\Apache Software Foundation\Tomcat 7.0



Step6: 完成,接下來編譯時選擇你要的執行環境即可


其他你感興趣的文章

Related Posts with Thumbnails