intra-mart Accel Platform Office 365 連携プログラミングガイド 第4版 2024-04-01

4. SharePoint Online のファイラを作成する (JavaEE開発モデル)

Office 365 連携機能 では、Microsoft 365 の SharePoint Online のドキュメントにアクセスするための以下の API を提供します。
  • Microsoft 365 のOneDrive REST APIをラップした低レベルAPI
    • jp.co.intra_mart.foundation.office365.api.v2_0.onedrive.operation.OneDriveOperation
  • intra-mart Accel Platform の Storageインタフェースを継承した高レベルAPI
    • jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage
ここでは、 Office 365 連携機能 の SharePointStorage APIを利用して実際にプログラムを作成する過程を説明します。

本項では、例として JavaEE開発モデル を利用しています。
また、サンプルコードは可読性を重視しているため、JSP内で全ての処理を行っています。

本項に記載のサンプルを組み合わせると 以下のような SharePoint Online のファイラが完成します。
SharePoint Online のファイラの完成イメージ
../../_images/sample.png

注意

Office 365 連携 のAPIの詳細については以下を参照してください。
Office 365 連携 のタグライブラリの詳細については以下を参照してください。

4.1. SharePoint Online 上にディレクトリを新規作成する

SharePoint Online 上にディレクトリを新規作成する実装のサンプルです。
リクエストパラメータとして受け取った プロバイダID、ターゲットのディレクトリパス、ディレクトリの名称 を元に新規ディレクトリを作成します。

実装例

SharePointStorage#makeDirectories() を利用します。

%CONTEXT_PATH%/sample/office365/filer/create_dir.jsp
 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
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="imui" uri="http://www.intra-mart.co.jp/taglib/imui" %>

<%@ page import="java.io.IOException" %>

<%@ page import="jp.co.intra_mart.common.platform.log.Logger" %>
<%@ page import="jp.co.intra_mart.foundation.context.Contexts" %>
<%@ page import="jp.co.intra_mart.foundation.context.model.AccountContext" %>
<%@ page import="jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage"%>

<%
  String userCd = Contexts.get(AccountContext.class).getUserCd();
  String providerId = request.getParameter("providerId");
  String currentDirPath = 
          request.getParameter("targetDirPath") != null ? request.getParameter("targetDirPath") : "/";
  String createDirName = 
          request.getParameter("createDirName") != null ? request.getParameter("createDirName") : null;
  String dirPath = currentDirPath + createDirName;

  String successMsg = null;
  String errorMsg = null;

  if (createDirName == null) {
    errorMsg = "ディレクトリ名が指定されていません。";
  } else {
    SharePointStorage storage = new SharePointStorage(userCd, providerId, dirPath);
    try {
      //ディレクトリの作成
      boolean result = storage.makeDirectories();
      if (result) {
        successMsg = "ディレクトリを作成しました。<br/><br/>" + dirPath;
      } else {
        errorMsg = "ディレクトリを作成できませんでした。<br/><br/>" + dirPath;
      }
    } catch (IOException e) {
      errorMsg = buildErrorMessage("ディレクトリ作成時にエラーが発生しました。", e);
    }
  }
%>
<imui:head>
<title>Office365 - SharePointStorage API サンプル - ディレクトリ作成</title>

<script type="text/javascript">
$(document).ready(function() {
  if(<%=successMsg != null%>){
    imuiShowSuccessMessage('<%=successMsg%>');
  }
  
  if(<%=errorMsg != null%>){
    imuiShowErrorMessage('<%=errorMsg%>');
  }
});

</script>
</imui:head>

<div class="imui-title">
  <h1><%=dirPath%></h1>
</div>

<div class="imui-toolbar-wrap">
  <div class="imui-toolbar-inner">
    <ul class="imui-list-toolbar">
      <li>
        <a href="javascript:$('#backToIndex').submit();" class="imui-toolbar-icon" title="一覧に戻る">
          <span class="im-ui-icon-common-16-back"></span>一覧に戻る
        </a>
      </li>
    </ul>
  </div>
</div>

<form  method="post" action="sample/office365/filer/index.jsp" id="backToIndex">
  <input type="hidden" name="targetDirPath" value="<%=currentDirPath%>"/>
</form>
<%!
  private static Logger logger = Logger.getLogger("sample.office365.filer.create_dir");
  private String buildErrorMessage(String errorMessage, Exception errorDetail) {
    logger.error(errorDetail.getMessage(), errorDetail);
    return errorMessage + "<br/><br/>" + errorDetail.getMessage();
  }
%>

注意

Microsoft社が提供する Microsoft 365 のWebAPIの動作仕様に起因し以下の制限があります。
SharePointStorage#makeDirectories() および OneDriveOperation#createFolderByPath() を利用しディレクトリを作成する場合、ルートディレクトリ直下に存在するディレクトリ名と同名のディレクトリを任意のディレクトリ配下に作成することはできません。

4.2. SharePoint Online 上にファイルをアップロードする

SharePoint Online 上にファイルをアップロードする実装のサンプルです。
マルチパートフォームデータとして受け取った プロバイダID、ターゲットのディレクトリパス、ファイルを元にファイルをアップロードします。

実装例

SharePointStorage#create() を利用します。

%CONTEXT_PATH%/sample/office365/filer/upload.jsp
 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
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>

<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.OutputStream" %>

<%@ page import="jp.co.intra_mart.foundation.context.Contexts" %>
<%@ page import="jp.co.intra_mart.foundation.context.model.AccountContext" %>
<%@ page import="jp.co.intra_mart.common.aid.javaee.http.MultipartFormData" %>
<%@ page import="jp.co.intra_mart.common.aid.javaee.http.MultipartFormData.Entity" %>
<%@ page import="jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage"%>
<%@ page import="jp.co.intra_mart.common.aid.jdk.java.io.IOUtil" %>

<%
  String userCd = Contexts.get(AccountContext.class).getUserCd();

  //Web Application Server によってはレスポンスにテーマが含まれてしまうため notheme を指定
  request.setAttribute("imui-theme-builder-module", "notheme");
  response.setContentType("json");

  MultipartFormData data = new MultipartFormData(request);
  String providerId = data.getEntity("providerId").getContent();
  String targetDirPath = new String(data.getEntity("targetDirPath").getBytes(), "utf-8");
  Entity localFileEntity = data.getEntity("local_file");
  String fileName = localFileEntity.getFileName();
  
  SharePointStorage storage = new SharePointStorage(userCd, providerId, targetDirPath + "/" + fileName);

  try (OutputStream outputStream = storage.create();
    InputStream inputStream = localFileEntity.getInputStream()
  ){
    IOUtil.transfer(inputStream, outputStream);
  }

  response.setContentType("json");
%>
[{
  "name":"<%= localFileEntity.getFileName() %>",
  "size":<%= localFileEntity.getContentLength() %>,
  "error":""
}]

4.3. SharePoint Online 上のファイルをダウンロードする

SharePoint Online 上のファイルをダウンロードする実装のサンプルです。
リクエストパラメータとして受け取った プロバイダID、ターゲットのファイルパス、ファイルの名称を元にファイルをダウンロードします。

実装例

SharePointStorage#open() を利用します。

%CONTEXT_PATH%/sample/office365/filer/download.jsp
 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
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="javax.activation.FileTypeMap" %>

<%@ page import="jp.co.intra_mart.common.aid.jdk.java.io.IOUtil" %>
<%@ page import="jp.co.intra_mart.foundation.context.Contexts" %>
<%@ page import="jp.co.intra_mart.foundation.context.model.AccountContext" %>
<%@ page import="jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage" %>
<%@ page import="jp.co.intra_mart.foundation.http.ResponseUtil" %>

<%
  String userCd = Contexts.get(AccountContext.class).getUserCd();
  String providerId = request.getParameter("providerId");
  String targetFilePath = request.getParameter("targetFilePath");
  String targetFileName = request.getParameter("targetFileName");

  response.resetBuffer();

  response.setCharacterEncoding(null);
  String mimeType = FileTypeMap.getDefaultFileTypeMap().getContentType(targetFileName);
  response.setContentType(mimeType);

  String encodedFileName = ResponseUtil.encodeFileName(request, "UTF-8", targetFileName);
  response.setHeader("Content-Disposition", "attachment; " + encodedFileName);

  SharePointStorage sharePointStorage = new SharePointStorage(userCd, providerId, targetFilePath);
  try (InputStream inputStream = sharePointStorage.open();
    OutputStream outputStream = response.getOutputStream();
  ) {
    IOUtil.transfer(inputStream, outputStream);
  }
%>

4.4. SharePoint Online 上のファイルの詳細情報を表示する

SharePoint Online 上のファイルの詳細情報を表示する実装のサンプルです。
リクエストパラメータとして受け取った プロバイダID、ターゲットのファイルパスをファイルの詳細情報を表示します。

実装例

%CONTEXT_PATH%/sample/office365/filer/show_detail.jsp
 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
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="imui" uri="http://www.intra-mart.co.jp/taglib/imui" %>
<%@ taglib prefix="imtag" uri="http://www.intra-mart.co.jp/taglib/core/standard" %>
<%@ taglib prefix="i18n" uri="http://www.intra-mart.co.jp/taglib/im-i18n" %>

<%@ page import="jp.co.intra_mart.foundation.context.Contexts" %>
<%@ page import="jp.co.intra_mart.foundation.context.model.AccountContext" %>
<%@ page import="jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage"%>

<%
  String userCd = Contexts.get(AccountContext.class).getUserCd();
  String providerId = request.getParameter("providerId");
  String targetPathForShowDetail = request.getParameter("targetPathForShowDetail");

  String errorMsg = "";
  if(targetPathForShowDetail == null){
    errorMsg = "ファイル、または、ディレクトリが指定されていません。";
  }

  SharePointStorage storage = new SharePointStorage(userCd, providerId, targetPathForShowDetail);
%>
<imui:head>
<title>Office365 - SharePointStorage API サンプル - 詳細表示</title>

<script type="text/javascript">
  $(document).ready(function() {
    if(<%= errorMsg != null %>){
      imuiShowErrorMessage('<%= errorMsg %>');
    }
  });
</script>
</imui:head>

<div class="imui-title">
  <imtag:condition validity="<%= Boolean.toString(errorMsg.isEmpty()) %>">
    <h1><%=storage.getCanonicalPath()%></h1>
  </imtag:condition>
</div>

<div class="imui-form-container-narrow">
  <div class="imui-chapter-title">
    <h2>詳細情報</h2>
  </div>
  <imtag:condition validity="<%= Boolean.toString(errorMsg.isEmpty()) %>">
    <table class="imui-table">
      <tr>
        <th class="wd-335px">正規化したパス</th>
        <td><%=storage.getCanonicalPath()%></td>
      </tr>
      <tr>
        <th>コンテンツをブラウザ上で確認可能なWeb URL</th>
        <td>
          <a href="<%=storage.getWebUrl()%>"><%=java.net.URLDecoder.decode(storage.getWebUrl(), "UTF-8")%></a>
        </td>
      </tr>
      <tr>
        <th>作成時刻</th>
        <td><i18n:dateTime value="<%=new java.util.Date(storage.created())%>" pattern="yyyy/MM/dd hh:mm:ss"/></td>
      </tr>
      <tr>
        <th>作成ユーザID</th>
        <td><%=storage.createdUserId()%></td>
      </tr>
      <tr>
        <th>作成ユーザ名</th>
        <td><%=storage.createdUserName()%></td>
      </tr>
      <tr>
        <th>最終更新時刻</th>
        <td><i18n:dateTime value="<%=new java.util.Date(storage.lastModified())%>" pattern="yyyy/MM/dd hh:mm:ss"/></td>
      </tr>
      <tr>
        <th>最終更新ユーザID</th>
        <td><%=storage.lastModifiedUserId()%></td>
      </tr>
      <tr>
        <th>最終更新ユーザ名</th>
        <td><%=storage.lastModifiedUserName()%></td>
      </tr>
      <tr>
        <th>ファイルサイズ</th>
        <td><%=storage.length()%>バイト</td>
      </tr>
    </table>
  </imtag:condition>
</div>

4.5. SharePoint Online 上のファイルを削除する

SharePoint Online 上のファイルを削除する実装のサンプルです。
リクエストパラメータとして受け取った プロバイダID、ターゲットのファイルパスを元にファイルを削除します。

実装例

SharePointStorage#remove() を利用します。

%CONTEXT_PATH%/sample/office365/filer/remove.jsp
 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
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="imui" uri="http://www.intra-mart.co.jp/taglib/imui" %>

<%@ page import="java.io.IOException" %>

<%@ page import="jp.co.intra_mart.common.platform.log.Logger" %>
<%@ page import="jp.co.intra_mart.foundation.context.Contexts" %>
<%@ page import="jp.co.intra_mart.foundation.context.model.AccountContext" %>
<%@ page import="jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage"%>

<%
  String userCd = Contexts.get(AccountContext.class).getUserCd();
  String providerId = request.getParameter("providerId");
  String targetPath = request.getParameter("targetPathForRemove");
  String currentDirPath = 
          request.getParameter("targetDirPath") != null ? request.getParameter("targetDirPath") : "/";

  String successMsg = null;
  String errorMsg = null;

  if (targetPath == null) {
    errorMsg = "ファイル、または、ディレクトリが指定されていません。";
  } else {
    SharePointStorage storage = new SharePointStorage(userCd, providerId, targetPath);
    try {
      //ファイルまたはディレクトリの削除
      boolean result = storage.remove(true);
      if (result) {
        successMsg = "「" + targetPath + "」を削除しました。";
      } else {
        errorMsg = "「" + targetPath + "」を削除できませんでした。";
      }
    } catch (IOException e) {
      errorMsg = buildErrorMessage("削除時にエラーが発生しました。", e);
    }
  }
%>
<imui:head>
<title>Office365 - SharePointStorage API サンプル - 削除</title>

<script type="text/javascript">
  $(document).ready(function() {
    if(<%=successMsg != null%>){
      imuiShowSuccessMessage('<%=successMsg%>');
    }
    if(<%=errorMsg != null%>){
      imuiShowErrorMessage('<%=errorMsg%>');
    }
  });
</script>
</imui:head>

<div class="imui-title">
  <h1><%=targetPath%></h1>
</div>

<div class="imui-toolbar-wrap">
  <div class="imui-toolbar-inner">
    <ul class="imui-list-toolbar">
      <li>
        <a href="javascript:$('#backToIndex').submit();" class="imui-toolbar-icon" title="一覧に戻る">
          <span class="im-ui-icon-common-16-back"></span>一覧に戻る
        </a>
      </li>
    </ul>
  </div>
</div>

<form  method="post" action="sample/office365/filer/index.jsp"  id="backToIndex">
  <input type="hidden" name="targetDirPath" value="<%=currentDirPath%>"/>
</form>

<%!
  private static Logger logger = Logger.getLogger("sample.office365.filer.remove");
  private String buildErrorMessage(String errorMessage, Exception errorDetail) {
    logger.error(errorDetail.getMessage(), errorDetail);
    return errorMessage + "<br/><br/>" + errorDetail.getMessage();
  }
%>

4.6. SharePoint Online 上のドキュメントをiframeで表示する

SharePoint Online 上のドキュメントをiframeで表示する実装のサンプルです。
リクエストパラメータとして受け取った プロバイダID、ターゲットのファイルパスを元にドキュメントをiframeで表示します。

実装例

im-office365 タグを利用します。

%CONTEXT_PATH%/sample/office365/filer/show_iframe.jsp
 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
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="imtag" uri="http://www.intra-mart.co.jp/taglib/core/standard" %>
<%@ taglib prefix="im-office365" uri="http://www.intra-mart.co.jp/taglib/im-office365"%>

<%
  String width = "640";
  String height = "480";
  
  String providerId = request.getParameter("providerId");
  String targetPath = request.getParameter("targetPathForShowIframe");

  String errorMsg = null;
  if (targetPath == null) {
    errorMsg = "ファイルが指定されていません。";
  }
%>
<imui:head>
<title>Office365 - iframe 表示</title>

<script type="text/javascript">
  $(document).ready(function() {
    if(<%=errorMsg != null%>){
      imuiShowErrorMessage('<%=errorMsg%>');
    }
  });
</script>
</imui:head>

<div class="imui-title">
  <h1><%=targetPath%></h1>
</div>

<div id="imui-container-inner">
  <imtag:condition validity="<%=isWord(targetPath)%>">
    <im-office365:word providerId="<%=providerId%>" path="<%=targetPath%>"
      width="<%=width%>" height="<%=height%>" />
  </imtag:condition>
  <imtag:condition validity="<%=isExcel(targetPath)%>">
    <im-office365:excel providerId="<%=providerId%>" path="<%=targetPath%>"
      width="<%=width%>" height="<%=height%>" />
  </imtag:condition>
  <imtag:condition validity="<%=isPowerPoint(targetPath)%>">
    <im-office365:powerPoint providerId="<%=providerId%>" path="<%=targetPath%>"
      width="<%=width%>" height="<%=height%>" />
  </imtag:condition>
</div>

<%!
  private String isWord(final String fileName) {
    return ".docx".equals(getExtension(fileName)) ? "true" : "false";
  }

  private String isExcel(final String fileName) {
    return ".xlsx".equals(getExtension(fileName)) ? "true" : "false";
  }

  private String isPowerPoint(final String fileName) {
    return ".pptx".equals(getExtension(fileName)) ? "true" : "false";
  }

  private String getExtension(String fileName) {
    final int indexOfLastDot = fileName.lastIndexOf(".");
    if (indexOfLastDot == -1) {
      return null;
    }
    return fileName.substring(indexOfLastDot);
  }
%>

注意

ドキュメントを表示するにはユーザが Microsoft 365 - SharePoint Online にログインしている必要があります。

4.7. SharePoint Online のファイラを作成する

前述の実装例を組み合わて SharePoint Online のファイラを作成します。

providerId は「 Office 365 連携 セットアップガイド 」で環境構築の際に設定した <im_office365_files_api.xml> のプロバイダIDを指定してください。

実装例

SharePointStorage#directoriesStorages()、SharePointStorage#filesStorages() を利用します。

%CONTEXT_PATH%/sample/office365/filer/index.jsp
  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
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="imui" uri="http://www.intra-mart.co.jp/taglib/imui" %>
<%@ taglib prefix="imtag" uri="http://www.intra-mart.co.jp/taglib/core/standard" %>

<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.io.OutputStream" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Collection" %>

<%@ page import="jp.co.intra_mart.common.platform.log.Logger" %>
<%@ page import="jp.co.intra_mart.foundation.context.Contexts" %>
<%@ page import="jp.co.intra_mart.foundation.context.model.AccountContext" %>
<%@ page import="jp.co.intra_mart.foundation.oauth.client.service.OAuthPreconditionException" %>

<%@ page import="jp.co.intra_mart.foundation.office365.service.storage.SharePointStorage"%>

<%
  String providerId = "Please_input_your_ID";
  String userCd = Contexts.get(AccountContext.class).getUserCd();
  String currentDirPath = 
          request.getParameter("targetDirPath") != null ? addSlashToTheEnd(request.getParameter("targetDirPath")) : "/";

  String errorMsg = null;
  SharePointStorage storage = new SharePointStorage(userCd, providerId, currentDirPath);

  // 一覧取得
  Collection<SharePointStorage> directoriesStorages = new ArrayList<SharePointStorage>();
  Collection<SharePointStorage> filesStorages = new ArrayList<SharePointStorage>();
  try {
    directoriesStorages = storage.directoriesStorages(false);
    filesStorages = storage.filesStorages(false);
  } catch (IOException e) {
    errorMsg = buildErrorMessage("ディレクトリ、または、ファイル一覧の取得時にエラーが発生しました。", e);
  }

  // 親ディレクトリの存在チェック
  String parentDir = storage.getParent();
  String existsParentDir = parentDir != null ? "true" : "false";
%>
<imui:head>
<title>Office365 - SharePointStorage API サンプル</title>
<script src="csjs/im_window.js"></script>
<script type="text/javascript">
  function moveDir(targetDirPath){
    $('#targetDirPath').val('<%= currentDirPath %>' + targetDirPath);
    $('#moveDirForm').submit();
  }

  function downloadFile(targetFilePath){
    $('#targetFilePath').val('<%= currentDirPath %>' + targetFilePath);
    $('#targetFileName').val(targetFilePath);
    $('#downloadFileForm').submit();
  }

  function removeFileOrDir(targetPath){
    $('#targetPathForRemove').val('<%= currentDirPath %>' + targetPath);
    $('#removeFileOrDirForm').submit();
  }

  function showDetail(targetPath){
    openNewWindow("office365_show_detail").focus();
    $('#targetPathForShowDetail').val('<%= currentDirPath %>' + targetPath);
    $('#showDetailForm').submit();
  }

  function showIframe(targetPath){
    openNewWindow("office365_show_iframe").focus();
    $('#targetPathForShowIframe').val('<%= currentDirPath %>' + targetPath);
    $('#showIframeForm').submit();
  }

$(document).ready(function() {
  if(<%= errorMsg != null %>){
    imuiShowErrorMessage('<%= errorMsg %>');
  }

  $('#toParent').click(function() {
    $('#targetDirPath').val('/' + '<%= parentDir %>');
    $('#moveDirForm').submit();
  });

});
</script>
</imui:head>
<div class="imui-container">
  <div class="imui-title">
      <h1><%= currentDirPath %></h1>
  </div>
  
  <div class="imui-form-container-narrow">
    <div class="imui-chapter-title">
        <h2>ファイル一覧</h2>
    </div>
    <form method="post" action="sample/office365/filer/index.jsp">
        <input type="text" name="targetDirPath" value="<%= currentDirPath %>" size="70" />
        <input type="submit" value="移動" class="imui-small-button" />
    </form>
    <br />
    <table class="imui-table">
      <tr>
        <th>名前</th>
        <th>詳細</th> 
        <th>削除</th> 
        <th>iframe表示</th> 
      </tr>
      <imtag:condition validity="<%= existsParentDir %>">
        <tr>
          <td><a href="javascript:void(0)" id="toParent">親ディレクトリへ移動</a></td>
          <td></td>
          <td></td>
          <td></td>
        </tr>
      </imtag:condition>

      <!-- ディレクトリ一覧 -->
      <imtag:repeat list="<%= directoriesStorages %>" type="SharePointStorage" item="dirStorage" index="idx">
        <tr>
          <td>
            <span class="im-ui-icon-common-16-folder"></span>
            <a href="javascript:void(0)"
              onclick="moveDir('<%= dirStorage.getName() %>');"><%= dirStorage.getName() %></a>
          </td>
          <td align="center">
            <a href="javascript:void(0)" onclick="showDetail('<%= dirStorage.getName() %>');">
                <span class="im-ui-icon-common-16-information"></span>
            </a>
          </td>
          <td align="center">
            <a href="javascript:void(0)" onclick="removeFileOrDir('<%= dirStorage.getName() %>');">
                <span class="im-ui-icon-common-16-trashbox"></span>
            </a>
          </td>
          <td></td>
        </tr>
      </imtag:repeat>
  
      <!-- ファイル一覧 -->
      <imtag:repeat list="<%= filesStorages  %>" type="SharePointStorage" item="fileStorage" index="idx">
        <tr>
          <td>
            <span class="im-ui-icon-common-16-document"></span>
            <a href="javascript:void(0)"
              onclick="downloadFile('<%= fileStorage.getName() %>');"><%= fileStorage.getName() %></a>
          </td>
          <td align="center">
            <a href="javascript:void(0)" onclick="showDetail('<%= fileStorage.getName() %>');">
              <span class="im-ui-icon-common-16-information"></span>
            </a>
          <td align="center">
            <a href="javascript:void(0)" onclick="removeFileOrDir('<%= fileStorage.getName() %>');">
              <span class="im-ui-icon-common-16-trashbox"></span>
            </a>
          </td>
          <td align="center">
            <imtag:condition validity="<%= isWordExcelPowerPointFile(fileStorage.getName()) %>">
              <a href="javascript:void(0)" onclick="showIframe('<%= fileStorage.getName() %>');">
                <span class="im-ui-icon-common-16-preview"></span>
              </a>
            </imtag:condition>
          </td>
        </tr>
      </imtag:repeat>

    </table>

    <br/>
    
    <div class="imui-chapter-title">
      <h2>ファイルアップロード</h2>
    </div>
    <form id="uploadForm" action="sample/office365/filer/upload.jsp" enctype="multipart/form-data">
      <imui:fileUpload outerFormId="uploadForm"/>
      <input type="hidden" name="targetDirPath" value="<%= currentDirPath %>" />
      <input type="hidden" name="providerId" value="<%= providerId %>" />
    </form>
    <div class="imui-chapter-title">
      <h2>ディレクトリ新規作成</h2>
    </div>
    
    <form method="POST" name="createDir" id="createDir"
      action="sample/office365/filer/create_dir.jsp">
      <input type="text" name="createDirName" />
      <input type="hidden" name="targetDirPath" value="<%= currentDirPath %>" />
      <input type="hidden" name="providerId" value="<%= providerId %>" />
      <input type="submit" value="作成" class="imui-small-button" />
    </form>
    
    <form method="POST" name="moveDirForm" id="moveDirForm"
      action="sample/office365/filer/index.jsp">
      <input type="hidden" name="targetDirPath" id="targetDirPath" />
      <input type="hidden" name="providerId" value="<%= providerId %>" />
    </form>
    
    <form method="POST" name="downloadFileForm" id="downloadFileForm"
      action="sample/office365/filer/download.jsp">
      <input type="hidden" name="targetFilePath" id="targetFilePath" />
      <input type="hidden" name="targetFileName" id="targetFileName" />
      <input type="hidden" name="providerId" value="<%= providerId %>" />
    </form>
    
    <form method="POST" name="removeFileOrDirForm" id="removeFileOrDirForm"
      action="sample/office365/filer/remove.jsp">
      <input type="hidden" name="targetPathForRemove" id="targetPathForRemove" />
      <input type="hidden" name="targetDirPath" id="targetDirPath" value="<%=currentDirPath%>"/>
      <input type="hidden" name="providerId" value="<%= providerId %>" />
    </form>
    
    <form method="POST" name="showDetailForm" id="showDetailForm"
      action="sample/office365/filer/show_detail.jsp" target="office365_show_detail">
      <input type="hidden" name="targetPathForShowDetail" id="targetPathForShowDetail" />
      <input type="hidden" name="targetDirPath" id="targetDirPath" value="<%=currentDirPath%>"/>
      <input type="hidden" name="providerId" value="<%= providerId %>" />
    </form>
    
    <form method="POST" name="showIframeForm" id="showIframeForm" 
      action="sample/office365/filer/show_iframe.jsp" target="office365_show_iframe">
      <input type="hidden" name="targetPathForShowIframe" id="targetPathForShowIframe" />
      <input type="hidden" name="targetDirPath" id="targetDirPath" value="<%=currentDirPath%>"/>
      <input type="hidden" name="providerId" value="<%= providerId %>" />
    </form>
    
  </div>
</div>

<%!
  private static Logger logger = Logger.getLogger("sample.office365.filer.index");

  private String buildErrorMessage(final String errorMessage, final Exception errorDetail){
    logger.error(errorDetail.getMessage(), errorDetail);
    if(errorDetail instanceof OAuthPreconditionException){
      return "「外部連携アプリケーション」画面でアプリケーションを許可してください。";
    } else {
      return errorMessage + "<br/><br/>" + errorDetail.getMessage();
    }
  }

  private String isWordExcelPowerPointFile(final String fileName) {
    final int indexOfLastDot = fileName.lastIndexOf(".");
    if (indexOfLastDot == -1) {
      return "false";
    }

    final String extension = fileName.substring(indexOfLastDot);
    if (".docx".equals(extension) || ".xlsx".equals(extension) || ".pptx".equals(extension)) {
      return "true";
    } else {
      return "false";
    }
  }
  private String addSlashToTheEnd(final String s) {
    final int indexOfLastSlash = s.lastIndexOf("/");
    if((indexOfLastSlash == -1) || (indexOfLastSlash == s.length() - "/".length())){
      return s;
    } else {
      return s + "/";
    }
  }
%>