001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016// package org.opengion.fukurou.model;
017package org.opengion.cloud;
018
019import java.io.File;
020import java.io.FileFilter;
021import java.io.FileNotFoundException;
022import java.io.IOException;
023import java.io.InputStream;
024
025import org.opengion.fukurou.system.FileOperation;                               // 8.5.6.0 (2024/02/29) package変更 fukurou.model → fukurou.system
026// import org.opengion.fukurou.model.CloudFileOperation;                // 8.0.0.2 (2021/10/15) fukurou.model → plugin.cloud にパッケージ移動
027import org.opengion.fukurou.system.FileOperationFactory;                // 8.5.6.0 (2024/02/29) package変更 fukurou.model → fukurou.system
028
029/**
030 * CloudFileOperation用のファイル情報の格納クラス
031 *
032 * listFilesで取得した、ディレクトリとファイル一覧情報を格納します。
033 *
034 * パフォーマンスや分かりやすさを考慮してCloudFileOperationからは分離しています
035 *
036 * @og.group ファイル操作
037 *
038 * @og.rev 5.10.8.0 (2019/02/01) 新規作成
039 * @og.rev 5.10.9.0 (2019/03/01) 変更対応
040 * @author oota
041 * @since JDK7.0
042 */
043public class FileOperationInfo extends CloudFileOperation {
044        /** このプログラムのVERSION文字列を設定します。{@value} */
045        private static final String VERSION = "8.5.4.2 (2024/01/12)" ;
046        private static final long serialVersionUID = 854220240112L ;
047
048        /** クラス変数 */
049        private final String plugin;
050
051        /** ファイルサイズ取得(Fileクラス) */
052        private long size;
053        // 8.0.0.0 (2021/07/31) Field ** has the same name as a method
054//      private long lastModified;
055//      private boolean isFile;
056//      private boolean isDirectory;
057        /** 最終時刻 */
058        private long lastTime;
059        /** ファイルかどうか */
060        private boolean isFil;
061        /** フォルダかどうか */
062        private boolean isDir;
063        /** ファイルオペレータ(クラウド系ファイルオブジェクト) */
064        private FileOperation file;
065
066        /**
067         * コンストラクタ
068         *
069         * 生成時の初期処理。
070         *
071         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
072         *
073         * @param plugin プラグイン名
074         * @param bucket バケット名
075         * @param path ファイルパス
076         */
077        public FileOperationInfo(final String plugin, final String bucket, final String path) {
078                super(bucket, path);
079                this.plugin     = plugin;
080                size            = 0;
081//              lastModified = 0;
082//              isFile = false;
083//              isDirectory = false;
084                lastTime        = 0L;
085                isFil           = false;
086                isDir           = false;
087                file            = null;
088        }
089
090        /**
091         * FileOperationクラスの遅延生成
092         *
093         * 呼び出し時に、FileOperationインスタンスが未生成の場合は、
094         * 生成を行います。
095         */
096        private void setFileOperation() {
097                if(file == null) {
098                        file = FileOperationFactory.newStorageOperation( plugin, conBucket, conPath );
099                }
100        }
101
102        /** Method */
103
104        /**
105         * 書き込み処理(評価用)
106         *
107         * Fileを書き込みます。
108         *
109         * @og.rev 8.0.0.1 (2021/10/08) 新規追加
110         *
111         * @param inFile 書き込みFile
112         * @throws IOException ファイル関連エラー情報
113         */
114        @Override                       // FileOperation
115        public void write(final File inFile) throws IOException {
116                setFileOperation();
117                file.write(inFile);
118        }
119
120        /**
121         * 書き込み処理
122         *
123         * InputStreamのデータを書き込みます。
124         *
125         * @param is 書き込みデータのInputStream
126         * @throws IOException ファイル関連エラー情報
127         */
128        @Override                       // FileOperation
129        public void write(final InputStream is) throws IOException {
130                setFileOperation();
131                file.write(is);
132        }
133
134        /**
135         * 読み込み処理
136         *
137         * データを読み込み、InputStreamとして、返します。
138         *
139         * @og.rev 8.5.4.2 (2024/01/12) throws を FileNotFoundException ⇒ IOException に変更
140         *
141         * @return 読み込みデータのInputStream
142         * @throws FileNotFoundException ファイル非存在エラー情報
143         */
144        @Override                       // FileOperation
145//      public InputStream read() throws FileNotFoundException {
146        public InputStream read() throws IOException {
147                setFileOperation();
148                return file.read();
149        }
150
151        /**
152         * 削除処理
153         *
154         * ファイルを削除します。
155         *
156         * @return 成否フラグ
157         */
158        @Override                       // File
159        public boolean delete() {
160                setFileOperation();
161                return file.delete();
162        }
163
164        /**
165         * コピー処理
166         *
167         * ファイルを指定先に、コピーします。
168         *
169         * @param afPath コピー先
170         * @return 成否フラグ
171         */
172        @Override                       // FileOperation
173        public boolean copy(final String afPath) {
174                setFileOperation();
175                return file.copy(afPath);
176        }
177
178        /**
179         * 一覧取得(Fileクラス)
180         *
181         * この抽象パス名が示すディレクトリ内のファイルを示す抽象パス名の配列を返します。
182         * 1つ下の、ディレクトリ・ファイル一覧を取得します。
183         *
184         * @param filter フィルタ情報
185         * @return ファイル一覧
186         * @see java.io.File#listFiles(FileFilter)
187         */
188        @Override                       // File
189        public File[] listFiles(final FileFilter filter) {
190                setFileOperation();
191                return file.listFiles(filter);
192        }
193
194        /**
195         * ファイルサイズ取得(Fileクラス)
196         *
197         * ファイルサイズを取得します。
198         *
199         * @return ファイルサイズ
200         * @see java.io.File#length()
201         */
202        @Override                       // File
203        public long length() {
204                return size;
205        }
206
207        /**
208         * ファイルサイズ設定
209         *
210         * ファイルサイズを設定します。
211         *
212         * @param size ファイルサイズ
213         */
214        protected void setSize(final long size) {
215                this.size = size;
216        }
217
218        /**
219         * 最終更新時刻の取得(Fileクラス)
220         *
221         * 最終更新時刻を取得します。
222         *
223         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
224         *
225         * @return 最終更新時刻
226         * @see java.io.File#lastModified()
227         */
228        @Override                       // File
229        public long lastModified() {
230//              return lastModified;
231                return lastTime;
232        }
233
234        /**
235         * 最終更新時刻の設定
236         *
237         * 最終更新時刻を設定します。
238         *
239         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
240         *
241         * @param lastModified 最終更新時刻
242         */
243        protected void setLastModifiedValue(final long lastModified) {
244//              this.lastModified = lastModified;
245                lastTime = lastModified;
246        }
247
248        /**
249         * ファイル判定取得(Fileクラス)
250         *
251         * ファイルであるかの判定を返します。
252         *
253         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
254         *
255         * @return ファイル判定
256         * @see java.io.File#isFile()
257         */
258        @Override                       // File
259        public boolean isFile() {
260//              return isFile;
261                return isFil;
262        }
263
264        /**
265         * ファイル判定設定
266         *
267         * ファイルであるかの判定を設定します。
268         *
269         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
270         *
271         * @param isFile ファイル判定
272         */
273        protected void setFile(final boolean isFile) {
274//              this.isFile = isFile;
275                isFil = isFile;
276        }
277
278        /**
279         * ディレクトリ判定取得(Fileクラス)
280         *
281         * ディレクトリであるかの判定を返します。
282         *
283         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
284         *
285         * @return ディレクトリ判定
286         * @see java.io.File#isDirectory()
287         */
288        @Override                       // File
289        public boolean isDirectory() {
290//              return isDirectory;
291                return isDir;
292        }
293
294        /**
295         * ディレクトリ判定設定
296         *
297         * ディレクトリであるかの判定を設定します。
298         *
299         * @og.rev 8.0.0.0 (2021/07/31) Field ** has the same name as a method
300         *
301         * @param isDirectory ディレクトリ判定
302         */
303        protected void setDirectory(final boolean isDirectory) {
304//              this.isDirectory = isDirectory;
305                isDir = isDirectory;
306        }
307
308        /**
309         * 親情報の取得(Fileクラス)
310         *
311         * 親情報を返します。
312         *
313         * @return 親情報
314         * @see java.io.File#getParentFile()
315         */
316        @Override                       // File
317        public File getParentFile() {
318//              return  FileOperationFactory.newStorageOperation( file , this.getParent() );
319                return  FileOperationFactory.resolveFile( file , this.getParent() );
320        }
321}