/*
 * Aipo is a groupware program developed by Aimluck,Inc.
 * Copyright (C) 2004-2008 Aimluck,Inc.
 * http://aipostyle.com/
 *
 * This program 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 com.aimluck.eip.libdocument;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.apache.cayenne.access.DataContext;
import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.exp.ExpressionFactory;
import org.apache.cayenne.query.SelectQuery;
import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;

import com.aimluck.commons.field.ALNumberField;
import com.aimluck.commons.field.ALStringField;
import com.aimluck.eip.cayenne.om.portlet.EipTWorkflowRoute;
import com.aimluck.eip.cayenne.om.security.TurbineUser;
import com.aimluck.eip.common.ALData;
//import com.aimluck.eip.document.util.DocumentUtils;
import com.aimluck.eip.libdocument.util.LibDocumentUtils;
import com.aimluck.eip.orm.DatabaseOrmService;
import com.aimluck.eip.util.ALEipUtils;
//import com.aimluck.eip.workflow.util.WorkflowUtils;

/**
 * 文書管理のファイルのResultDataです。 <BR>
 *
 */
public class DocumentFileResultData implements ALData {
  
  /** logger */
  private static final JetspeedLogger logger = JetspeedLogFactoryService
      .getLogger(DocumentFileResultData.class.getName());

  /** ファイル ID */
  private ALNumberField file_id;

  /** ファイルタイトル */
  private ALStringField file_title;

  /** ファイル名 */
  private ALStringField file_name;

  /** ファイルサイズ */
  private ALNumberField file_size;

  /** ファイル位置 */
  private String position;

  /** メモ */
  private ALStringField note;

  /** 登録者名 */
  private ALStringField create_user;

  /** 更新者名 */
  private ALStringField update_user;

  /** 登録日 */
  private ALStringField create_date;

  /** 更新日 */
  private ALStringField update_date;

  /** フォルダ ID */
  private ALNumberField folder_id;

  /** フォルダ名 */
  private ALStringField folder_name;

  /** 編集可能か */
  private boolean is_editable;
  
  //20100406 追加
  /** バージョン */
  private ALNumberField file_rev_mj;
  
  private ALNumberField file_rev_mn;

  /** 公開終了日 */
  private ALStringField open_end_date;
  
  /** 保存終了日 */
  private ALStringField save_end_date;
  
  /** チェックインコメント */
  private ALStringField checkin_comment;
  
  /** チェックアウト者 */
  private ALStringField checkout_user;

  /** チェックアウト者 ID */
  private ALNumberField checkout_user_id;
  
  /** チェックアウト日 */
  private ALStringField checkout_date;
  
  /** 申請者 */
  private ALStringField request_user;

  /** 申請者 ID */
  private ALNumberField request_user_id;

  /** 申請種類 */
  private ALStringField request_type;
  
  /** Relation ファイル名 */
  private ArrayList rel_file_name;
  
  /** Relation ファイルID */
  private ALNumberField rel_file_id;

  /** チェックアウト状態 */
  private int checkout_status;

  /** 申請状態 */
  private int request_status;
  
  /** アクセス権限 */
  /** メンバー選択が必要かどうか */
  private boolean isMemberRead;
  private boolean isMemberEdit;

  /** 閲覧権限メンバーグループリスト */
  private ArrayList readGroupList;
  
  /** 閲覧権限メンバーユーザーリスト */
  private ArrayList readUserList;
  
  /** 編集権限メンバーグループリスト */
  private ArrayList editGroupList;
  
  /** 編集権限メンバーユーザーリスト */
  private ArrayList editUserList;

  /** 申請中か */
  private boolean isRequesting;
  
  /** 再申請の条件を満たすか */
  private boolean isReqAgain;
  
  /** 申請経路一覧 */
  private List routeList;

  /** 申請経路ID */
  private ALNumberField route_id;

  private ALStringField route;
  
  /** 申請経路の選択 */
  private boolean is_saved_route;
  
  /**
   *
   * @see com.aimluck.eip.common.ALData#initField()
   */
  public void initField() {
    file_id = new ALNumberField();
    file_title = new ALStringField();
    file_name = new ALStringField();
    file_size = new ALNumberField();
    note = new ALStringField();
    note.setTrim(false);
    create_user = new ALStringField();
    update_user = new ALStringField();
    create_date = new ALStringField();
    update_date = new ALStringField();
    folder_id = new ALNumberField();
    folder_name = new ALStringField();
    //20100406 追加
    file_rev_mj = new ALNumberField();
    file_rev_mn = new ALNumberField();
    open_end_date = new ALStringField();
    save_end_date = new ALStringField();
    checkin_comment = new ALStringField();
    checkin_comment.setTrim(false);
    checkout_user = new ALStringField();
    checkout_user_id = new ALNumberField();
    checkout_date = new ALStringField();
    request_user = new ALStringField();
    request_user_id = new ALNumberField();
    request_type = new ALStringField();
    rel_file_name = new ArrayList();
    rel_file_id = new ALNumberField();

    readGroupList = new ArrayList();
    readUserList = new ArrayList();
    editGroupList = new ArrayList();
    editUserList = new ArrayList();
    
    routeList = new ArrayList();
    route_id = new ALNumberField();
    route = new ALStringField();
    
    // デフォルトの申請経路があれば設定する。
    /*
    EipTWorkflowRoute docRoute = DocumentUtils.getDocRoute();
    if(docRoute != null){
      is_saved_route = true;
      route_id.setValue(docRoute.getRouteId());
      route.setValue(docRoute.getRoute());
      //DocumentUtils.setWorkflowMember(workflowMemberList, route.getValue());
    }
    */
    
  }

  /**
   * @return
   */
  public ALNumberField getFileId() {
    return file_id;
  }

  /**
   * @return
   */
  public ALStringField getFileTitle() {
    return file_title;
  }

  /**
   * @return
   */
  public ALStringField getFileName() {
    return file_name;
  }

  /**
   * @return
   */
  public ALNumberField getFileSize() {
    return file_size;
  }

  /**
   * @param i
   */
  public void setFileId(long i) {
    file_id.setValue(i);
  }

  /**
   * @param string
   */
  public void setFileTitle(String string) {
    file_title.setValue(string);
  }

  /**
   * @param string
   */
  public void setFileName(String string) {
    file_name.setValue(string);
  }

  /**
   * @param i
   */
  public void setFileSize(long i) {
    file_size.setValue(i);
  }

  /**
   * @return
   */
  public String getNote() {
    return ALEipUtils.getMessageList(note.getValue());
  }

  /**
   * @param string
   */
  public void setNote(String string) {
    note.setValue(string);
  }

  public ALStringField getCreateUser() {
    return create_user;
  }

  public void setCreateUser(String str) {
    create_user.setValue(str);
  }

  public ALStringField getUpdateUser() {
    return update_user;
  }

  public void setUpdateUser(String str) {
    update_user.setValue(str);
  }

  /**
   * @return
   */
  public ALStringField getCreateDate() {
    return create_date;
  }

  /**
   * @return
   */
  public ALStringField getUpdateDate() {
    return update_date;
  }

  /**
   * @param string
   */
  public void setCreateDate(String string) {
    create_date.setValue(string);
  }

  /**
   * @param string
   */
  public void setUpdateDate(String string) {
    update_date.setValue(string);
  }

  public String getPosition() {
    return position;
  }

  public void setPosition(String str) {
    position = str;
  }

  public ALNumberField getFolderId() {
    return folder_id;
  }

  public void setFolderId(int id) {
    folder_id.setValue(id);
  }

  public ALStringField getFolderName() {
    return folder_name;
  }

  public void setFolderName(String string) {
    folder_name.setValue(string);
  }

  public void setisEditable(boolean isEditable) {
    this.is_editable = isEditable;
  }

  public boolean isEditable() {
    return is_editable;
  }
  
  //20100406
  /**
   * @return
   */
  public ALNumberField getFileRevMj() {
    return file_rev_mj;
  }

  /**
   * 
   * @param i
   */
  public void setFileRevMj(int i) {
    file_rev_mj.setValue(i);
  }

  /**
   * 
   * @return
   */
  public ALNumberField getFileRevMn() {
    return file_rev_mn;
  }
  
  /**
   * 
   * @param i
   */
  public void setFileRevMn(int i) {
    file_rev_mn.setValue(i);
  }

  public ALStringField getOpenEndDate() {
    return open_end_date;
  }

  /**
   * @param string
   */
  public void setOpenEndDate(String string) {
    open_end_date.setValue(string);
  }
  
  public ALStringField getSaveEndDate() {
    return save_end_date;
  }

  /**
   * @param string
   */
  public void setSaveEndDate(String string) {
    save_end_date.setValue(string);
  }
  
  /**
   * @return
   */
  public String getCheckinComment() {
    return ALEipUtils.getMessageList(checkin_comment.getValue());
  }

  /**
   * @param string
   */
  public void setCheckinComment(String string) {
    checkin_comment.setValue(string);
  }
  
  public ALStringField getCheckoutUser() {
    return checkout_user;
  }

  public void setCheckoutUser(String str) {
    checkout_user.setValue(str);
  }

  public ALStringField getRequestUser() {
    return request_user;
  }

  public void setRequestUser(String str) {
    request_user.setValue(str);
  }

  public ALNumberField getCheckoutUserId() {
    return checkout_user_id;
  }

  public void setCheckoutUserId(long i) {
    checkout_user_id.setValue(i);
  }
  
  public ALStringField getRequestType() {
    return request_type;
  }
  
  public int getRequestTypeInt() {
    return Integer.valueOf(request_type.getValue());
  }

  public void setRequestType(String str) {
    request_type.setValue(str);
  }

  public ALNumberField getRequestUserId() {
    return request_user_id;
  }

  public void setRequestUserId(long i) {
    request_user_id.setValue(i);
  }
  
  public ALStringField getCheckoutDate() {
    return checkout_date;
  }
  
  public void setCheckoutDate(String string) {
    checkout_date.setValue(string);
  }
  
  public ArrayList getRelFileName() {
    return rel_file_name;
  }
  
  public void setRelFileName(ArrayList list) {
    rel_file_name = list;
  }

  public ALNumberField getRelFileID() {
    return rel_file_id;
  }
  
  public void setRelFileID(int i) {
    rel_file_id.setValue(i);
  }

  public int getCheckoutStatus() {
    return checkout_status;
  }

  public void setCheckoutStatus(int CheckoutStatus) {
    checkout_status = CheckoutStatus;
  }

  public int getRequestStatus() {
    return request_status;
  }

  public void setRequestStatus(int RequestStatus) {
    request_status = RequestStatus;
  }

  public boolean getIsMemberRead() {
    return (isMemberRead);
  }
  
  public void setIsMemberRead(boolean IsMemberRead) {
    isMemberRead = IsMemberRead;
  }
  
  public boolean getIsMemberEdit() {
    return (isMemberEdit);
  }
    
  public void setIsMemberEdit(boolean IsMemberEdit) {
    isMemberEdit = IsMemberEdit;
  }

  public ArrayList getReadGroupList() {
    return readGroupList;
  }
  
  public void setReadGroupList(ArrayList list) {
    readGroupList = list;
  }

  public ArrayList getReadUserList() {
    return readUserList;
  }
  
  public void setReadUserList(ArrayList list) {
    readUserList = list;
  }

  public ArrayList getEditGroupList() {
    return editGroupList;
  }
  
  public void setEditGroupList(ArrayList list) {
    editGroupList = list;
  }

  public ArrayList getEditUserList() {
    return editUserList;
  }
  
  public void setEditUserList(ArrayList list) {
    editUserList = list;
  }

  public boolean getIsRequesting() {
    return isRequesting;
  }

  public void setIsRequesting(boolean isRequesting) {
    this.isRequesting = isRequesting;
  }

  public List getRouteList() {
    return routeList;
  }

  public void setRouteList(List routeList) {
    this.routeList = routeList;
  }

  public ALNumberField getRouteId() {
    return route_id;
  }

  public void setRouteId(ALNumberField route_id) {
    this.route_id = route_id;
  }

  public ALStringField getRoute() {
    return route;
  }

  public void setRoute(ALStringField route) {
    this.route = route;
  }

  public boolean getIsSavedRoute() {
    return is_saved_route;
  }

  public void setIsSavedRoute(boolean is_saved_route) {
    this.is_saved_route = is_saved_route;
  }
  
  public String getRouteH() {
    StringBuffer routeun = new StringBuffer();
    String username;

    if (route.getValue() != null && !"".equals(route.getValue())) {
      StringTokenizer st = new StringTokenizer(route.getValue(), ",");
      while (st.hasMoreTokens()) {
        username = getName(st.nextToken());
        routeun.append(username);
        routeun.append(" -> ");
      }
      routeun.append("完了");

      return routeun.toString();
    }
    return "";
  }

  public boolean getIsReqAgain() {
    return isReqAgain;
  }

  public void setIsReqAgain(boolean isReqAgain) {
    this.isReqAgain = isReqAgain;
  }
  
  /**
   * 指定した ID に対するユーザの名前を取得する．
   *
   * @param userId
   * @return
   */
  public static String getName(String userId) {
    if (userId == null || userId.equals(""))
      return null;

    String userName = null;
    String firstName = null;
    String lastName = null;
    StringBuffer buffer = new StringBuffer();

    try {
      DataContext dataContext = DatabaseOrmService.getInstance()
          .getDataContext();
      SelectQuery query = new SelectQuery(TurbineUser.class);
      Expression exp = ExpressionFactory.matchDbExp(
          TurbineUser.USER_ID_PK_COLUMN, Integer.valueOf(userId));
      query.setQualifier(exp);
      List destUserList = dataContext.performQuery(query);
      if (destUserList == null || destUserList.size() <= 0)
        return null;
      userName = ((TurbineUser) destUserList.get(0)).getLoginName();
      firstName = ((TurbineUser) destUserList.get(0)).getFirstName();
      lastName = ((TurbineUser) destUserList.get(0)).getLastName();
    } catch (Exception ex) {
      logger.error("Exception", ex);
      return null;
    }
    return buffer.append(lastName).append(" ").append(firstName).toString();
  }
  
}
