//{HEADER
/**
* This class is part of jnex 'Nexirius Application Framework for Java'
*
* Copyright (C) Nexirius GmbH, CH-4450 Sissach, Switzerland (www.nexirius.ch)
*
* <p>This library is free software; you can redistribute it and/or<br>
* modify it under the terms of the GNU Lesser General Public<br>
* License as published by the Free Software Foundation; either<br>
* version 2.1 of the License, or (at your option) any later version.</p>
*
* <p>This library is distributed in the hope that it will be useful,<br>
* but WITHOUT ANY WARRANTY; without even the implied warranty of<br>
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU<br>
* Lesser General Public License for more details.</p>
*
* <p>You should have received a copy of the GNU Lesser General Public<br>
* License along with this library; if not, write to the Free Software<br>
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</p>
* </blockquote>
*
* <p>
* Nexirius GmbH, hereby disclaims all copyright interest in<br>
* the library jnex' 'Nexirius Application Framework for Java' written<br>
* by Marcel Baumann.</p>
*/
//}HEADER
package com.nexirius.tools.dirsync;
import com.nexirius.framework.datamodel.*;
import com.nexirius.util.XFile;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
public class FileInfoModel extends StructModel {
protected StringModel name;
protected LongModel hash;
protected LongModel size;
protected DirSyncStatusModel status;
public static final String FIELD_name = "name";
public static final String FIELD_size = "size";
public static final String FIELD_hash = "hash";
public static final String FIELD_NAME = "FileInfo";
public FileInfoModel(String fieldName, String name) {
super(fieldName);
init(name);
}
public FileInfoModel(String name) {
super(FIELD_NAME);
init(name);
}
public FileInfoModel() {
this("");
}
public FileInfoModel(FileInfo fileInfo) {
this(fileInfo.name);
hash.setLong(fileInfo.hash);
size.setLong(fileInfo.size);
}
private void init(String name) {
append(this.name = new StringModel(name, FIELD_name));
size = new LongModel(0, FIELD_size);
append(size);
hash = new LongModel(0, FIELD_hash);
append(hash);
status = new DirSyncStatusModel();
append(status);
}
public void setCreate() {
status.setCreate();
}
public void setChange() {
status.setChange();
}
public void setRemove() {
status.setRemove();
}
public boolean isCreate() {
return status.isCreateStatus();
}
public boolean isChange() {
return status.isChangeStatus();
}
public boolean isRemove() {
return status.isRemoveStatus();
}
public String getName() {
return name.getText();
}
public boolean sameVersion(FileInfoModel targetFile) {
return getHash() == targetFile.getHash(); //To change body of created methods use File | Settings | File Templates.
}
private long getHash() {
return hash.getLong();
}
public void sync(String sourceDirUrl, String targetRootDirectory, String directoryName, DirSyncManager dirSyncManager) {
if (dirSyncManager.isInterrupted()) {
return;
}
if (isCreate() || isChange()) {
String targetDir = targetRootDirectory + XFile.separator + directoryName;
XFile targetFile = new XFile(targetDir, getName());
URL url = null;
try {
url = new URL(sourceDirUrl + '/' + getName());
dirSyncManager.createFile(url, targetFile);
} catch (MalformedURLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} else if (isRemove()) {
dirSyncManager.removeFile(targetRootDirectory, directoryName, getName());
}
}
public String getPath() {
DirectoryInfoModel parentDirectoryModel = getParentDirectoryInfoModel();
if (parentDirectoryModel != null) {
return parentDirectoryModel.getPath() + getName();
}
return getName();
}
public DirSyncStatusModel getDirSyncStatus() {
return status;
}
public DirectoryInfoModel getParentDirectoryInfoModel() {
DataModel parentDataModel = getParentDataModel();
if (!(parentDataModel instanceof ArrayModel)) {
return null;
}
DataModel ret = parentDataModel.getParentDataModel();
if (!(ret instanceof DirectoryInfoModel)) {
return null;
}
return (DirectoryInfoModel)ret;
}
}