星期二, 2月 28, 2012

[Javascript] Javascript實作Trim方法

取代字串的前後空白字元正則表示式

var re = /(^[\s]*)|([\s]*$)/g;
var newname = name.replace(re,"");
左空白
var re = /(^[\s]*)/g;
右空白
var re = /([\s]*$)/g;

[AD] Naming convention

整合AD服務常常遇到要查的命名原則問題!!請服用
Naming conventions in Active Directory for computers, domains, sites, and OUs

星期三, 2月 22, 2012

[Javascript] i18n問題: 可參數化的翻譯語句

先前在實作i18n上未考慮翻譯字串加上參數順序的問題
會導致不同語言翻譯下參數順序會被改變的問題!!
因此要改寫一下原本的printf方法!!

參數命名規則:  _%s<參數ID>_  , ID由1開始
參數命名範例:  Hello _%s1_ !!

星期二, 2月 21, 2012

[jQuery API] 事件委派(delegate)使用


最近又用到delegate來綁定事件,怕又忘記順便記一下。

//action binding
$(".manage_act").delegate(".doedit", "click", function () {
    //dosomething
});

每次頁面重新畫UI的時候必需先undelegate,不然會異常(重覆綁定事件)
//must reset previous delegate
$myGroup.undelegate("click");



[Java] PreparedStatement Order by 失效問題!!

使用PreparedStatement操作Order by指令無法得到排序的解法!!
參考Stackoverflow:PreparedStatement not returning ordered ResultSet 至一文。

範例:
透過PreparedStatement來執行以下sql子句

星期一, 2月 20, 2012

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


基於以下理由所以選擇java-backed web script的作法:
1.accessing alfresco application services not available via JavaScript API
2.when the performance is absolutely critical
3.to get tighter control of the generated response.
4.when we need to access the Alfresco API
5.if we prefer stronger programming language like JAVA

透過上面的架構圖,我們可以知道需要準備四個元件
1.XML descriptor document (Describing the webscript controller)
2.Freemarker (Writing the response template-FTL)
3.Java class (Writing the webscript)
4.Spring bean XML(Register the Java class in the Alfresco)

接著準備來寫一個Java Backed Web Script吧~
[Alfresco] How to create Java backed web script in alfresco (2)

Reference:
Alfresco Help Java backed web script
Alfresco WiKi: Java backed web script samples
Alfresco Blog: Java-Backed Web Scripts
Web script article

星期三, 2月 15, 2012

[Mysql] left join to count rows

計算聯絡人清單的個數,
分別有contact_list與contact_list_member,
使用left join去計算每個聯絡群組的總個數!!
使用inner join會讓無成員的聯絡人群組無法找出來!!
記得group by cl.contact_list_id也是重點!!

SELECT cl.contact_list_id,cl.contact_list_name,cl.creator,cl.create_date,Count(m.contact_list_id) total FROM `contact_list` cl
left join contact_list_member m on m.contact_list_id = cl.contact_list_id
group by  cl.contact_list_id

如果要再加入條件限制!!

select * from
(
SELECT cl.contact_list_id,cl.contact_list_name,cl.creator,cl.create_date,Count(m.contact_list_id) total FROM `contact_list` cl left join contact_list_member m on m.contact_list_id = cl.contact_list_id
group by cl.contact_list_id
) new_contact_list
where total >1

星期二, 2月 14, 2012

[MS SQL] Paging 的語法

跟MSSQL的分頁指令蠻不一樣的,
在MySQL用limit的方法就可以簡單達成!!

$per_page=20;
$page=2;
$first_result=($page-1)*$per_page;
"select * from contacts limit $first_result, $per_page order by last_name;";

"select * from contacts order by last_name limit $first_result, $per_page ;";

//自已的範例加了order by

select * from contact_list where order by create_date limit 1,1

尚未考慮最佳解法!!

星期四, 2月 09, 2012

[Java] Httpurlconnection timeout issue

如何使用HttpUrlConnection的Request TIMEOUT例外處理?

故事是剛剛同事打電話來說,目前實作的REST CLIENT對於TIMEOUT的
Request無法有效擷取到例外事件處理!!
找到以下這篇討論也是解法:Java URLConnection Timeout


try{
URL url = new URL("http://www.myurl.com/sample.xml");
          URLConnection urlConn = url.openConnection();
          urlConn.setConnectTimeout(15000);
          urlConn.setReadTimeout(15000);
          urlConn.setAllowUserInteraction(false);         
          urlConn.setDoOutput(true);

          InputStream inStream = urlConn.getInputStream();
          InputSource input = new InputSource(inStream);
}catch(SocketTimeoutException e){

}catch(IOException e){
}


要自已設這二個參數然後在擷SocketTimeoutException
urlConn.setConnectTimeout(connectTimeout);
urlConn.setReadTimeout(socketTimeout);

另外一個方法擷至此文(JDK中的URLConnection參數詳解):
HttpURLConnection是基于HTTP協議的,其底層通過socket通信實現。如果不設置超時(timeout),在網絡異常的情況下,可能會導致程序僵死而不繼續往下執行。可以通過以下兩個語句來設置相應的超時 System.setProperty("sun.net.client.defaultConnectTimeout", 超時毫秒數字符串); System.setProperty("sun.net.client.defaultReadTimeout", 超時毫秒數字符串);
其中: sun.net.client.defaultConnectTimeout:連接主機的超時時間(單位:毫秒) sun.net.client.defaultReadTimeout:從主機讀取數據的超時時間(單位:毫秒)
例如: System.setProperty("sun.net.client.defaultConnectTimeout", "30000"); System.setProperty("sun.net.client.defaultReadTime
Java中可以使用HttpURLConnection來請求WEB資源。 HttpURLConnection對象不能直接構造,需要通過URL.openConnection()來獲得HttpURLConnection

明天再來修正一下寫的CODE~

[Java] Insert if not exist: 資料列不存在時才新增


重覆的連絡人清單就不要插入!!
重點在select * from (select 2,'ken') as tmpInsert記得要重新給一個別名,
不然編譯都會爆炸給你看lol

範例:
插入一個使用者ken到聯絡人清單索引為2的結果

insert into contact_list_member(contact_list_id,uid)
select * from (select 2,'ken') as tmpInsert
where not exists(select 1 from contact_list_member clm where clm .contact_list_id= 2 and  clm .uid='ken')


使用preparedstatement就會像下面那樣
//insert if not exist
   String insertMemberSql= 
    "insert into contact_list_member(contact_list_id,uid) select * from (select ?,?) as tmpInsert where not exists(select * from contact_list_member where contact_list_id=? and uid=?)";

Update 2012/03/08 今天在同事的測試下發現上面的語句在插入的值都一樣的時候所產生的duplicate column name bug,簡單的用新增使用者的範例來記綠一下 

範例:
以下語句要新增一個帳號跟密碼都是叫cindy,不一樣就不會有這個問題!!

insert into users(account,password) select* from (select 'cindy','cindy') as tmpInsert where not exists(select 1 from users where account = 'cindy')

 問題: 錯誤的原因在於我們要建立一個帳號跟密碼一樣的使用者,會造成表格出現重覆欄位名稱的問題(Duplicate column name 'cindy'錯誤)

 以下是產生這個錯誤的SQL片段

select* from (select 'cindy','cindy') as tmpInsert 解法

因tmpInsert已經含有重覆的column name了,因此我們需要指定tmpInsert內的column name,才不會讓表格內的欄位名稱重覆的異常

 select account ,password from (select 'cindy' as account ,'cindy' as password) as tmpInsert

上述語句會產生的tmpInsert資料表結果集

account  |  password
cindy     |  cindy

Reference: MySQL一個聰明的sql,當Record沒有的時候才增加
不存在时才插入数据

星期二, 2月 07, 2012

[Java] JDBC ResultSet/RowSet 取得查詢結果的總列數

常常需要得到查詢結果返回的總列數,
Java提供以下三種解法:

[Java] 必學!! 取得當前新增資料的索引值

當在MySQL使用AUTO_INCREMENT產生索引的值,如何取得新增的索引值呢?

MySQL提供了LAST_INSERT_ID()解決這個問題!!

SELECT LAST_INSERT_ID();


如果要用JDBC實作的話只需要加入Statement.RETURN_GENERATED_KEYS,
執行getGeneratedKeys()即可輕鬆取得!!

星期一, 2月 06, 2012

[Alfresco] Boolean type is in FTL

在ftl檔案遇到boolean欄位一開始所遇到的錯誤

{
"shareDpxStatus": <#if person.properties.shareDpxStatus??>${person.properties.shareDpxStatus}<#else>null
}


原來ftl檔不支援直接將boolean型態印出來,要先轉換成字串型態
解法如下:
{
"shareDpxStatus": <#if person.properties.shareDpxStatus??>${person.properties.shareDpxStatus?string("true","false")}<#else>null
}

星期日, 2月 05, 2012

[Java] JDBC Transaction

本篇說明如何在JDBC實作Transaction機制。

步驟一:MsSQL設定:
設定INNODB資料表

Tip:MySQL 對不同的 Table 可以選擇不同的 Implementation 方式,像是最早的 ISAM (ISAM 將在 MySQL 5.0 以後被移除),後來的 MyISAM (因為要做 Transaction 時要預留多一點空間來進行處理,因此如果不須使用 Transaction 時,直接建 Table 就會採用 MyISAM 的格式)、把 Table 存在記憶體中提升效能的 HEAP Table,以及有支援 Transaction 的 INNODB 格式。
步驟二:如何在Java code執行Transcation

星期二, 1月 31, 2012

星期一, 1月 30, 2012

[Alfresco] Pagination of Lucene

使用javascript API的search.query(searchParameters)方法時,
當skipCount超過一千筆時,會無法取得回傳的結果,
導致客製化分頁會失敗。

找到以下這篇跟我遇到一樣的問題:
Improve the skipCount function not to check the permissions.

記錄一下:
This is an enhancement request for paging offset in FTS query called "skipcount". Alfresco don't have to check permission of skipped items when we specify the skipCount. 

(*1 system.max.permissionChecks 1000 as the default) 

I know the paging search itself won't be affected by the permission check as far as I tested it with an out of box webscripts named "children.get. js". So, in this case it works very fine, because this webscript uses "group.getChildGroups(maxItems, skipCount)" with ModelUtil.paging internally, so with this webscripts I can correctly get the result more than the specified number to the permission check. I attached the webscript, please find the sample-webscripts.zip for your reference. 


But, the problem is that if we use the paging offset in FTS query called "skipcount" combined with the Lucene search query in the WebScripts as follows, then it will be affected by the permission check. I attached the webscirpts named paging-result.zip for your reference. 

    var skipCount = 0 + args["skip"]; 
    var searchParams = {}; 
    searchParams.query = "cm\:name:document*"; 
    searchParams.language = "fts-alfresco"; 
    var paging = {}; 
    paging.maxItems = 100; 
    paging.skipCount = skipCount; 
    paging.totalItems = 100, 
    searchParams.page = paging; 
    var results = search.query(searchParams); 
    model["length"] = results.length; 
    model["results"] = results; 

In this case, when we specify the skipcount below the number of system.max.permissionCheck for example 1000 as the default, and set a proper paging value, then it will return the correct result, but the problem is when we specify skipcount over 1,000, then webscripts returns no results (zero items). 
So, the work around is to set over 1,000 to the "system.acl.maxPermissionChecks" , then we can get the results correctly. But increasing this parameter will give more stress to Alfresco server, so that would be nice if we could improve the function of skipCount since Alfresco don't have to check the permission of skipped items when we specify the skipCount. 


請在repository.properties修正以下二個參數
#
# Properties to limit resources spent on individual searches
#
# The maximum time spent pruning results
system.acl.maxPermissionCheckTimeMillis=100000
# The maximum number of results to perform permission checks against
system.acl.maxPermissionChecks=10000

不過當存取超過10000的時候發生以下例外錯誤!!(待續)
目前限制只能取到一層的一萬筆,當該層超過一萬筆後就拿不到資料!!
Transactional update cache 'org.alfresco.cache.node.aspectsTransactionalCache' is full (10000)

其他你感興趣的文章

Related Posts with Thumbnails