//{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.framework.datamodel.persistence;
import com.nexirius.framework.datamodel.DataModel;
import com.nexirius.framework.datamodel.DataModelEvent;
import com.nexirius.framework.datamodel.DataModelListener;
import com.nexirius.framework.datamodel.DataModelVector;
import com.nexirius.util.StringVector;
import com.nexirius.util.XFile;
import java.util.HashMap;
import java.util.Iterator;
/**
* This class is used a for storing and loading persistent datamodels. Each persistent datamodel
* lives in an environment
*
* @author Marcel Baumann
*/
public class DirectoryEnvironment extends PersistenceEnvironment {
String directory;
String extension = null;
boolean relative = true;
StringVector files = null;
boolean useMagicNumber = false;
HashMap needSaving = new HashMap();
ChangeListener changeListener = new ChangeListener();
/**
* @param name The symbolic name of the environment
* @param directory The name of the directory
* @param relative if false then the directory is absolute (ignoring rootDirectory)
*/
public DirectoryEnvironment(String name, String directory, boolean relative) {
super(name);
this.directory = directory;
this.relative = relative;
}
public void setUseMagicNumber(boolean on) {
useMagicNumber = on;
}
public boolean useMagicNumber() {
return useMagicNumber;
}
public String getExtension() {
return extension;
}
public ChangeListener getChangeListener() {
return changeListener;
}
/**
* Define a filename extension which is used to extract files with a specific
* name extension (the '.' is not appended automatically). If the extension null then
* the default extension (FileSystemPersistence.DEFAULT_FILE_EXTENSION) is used instead.
*/
public void setExtension(String extension) {
this.extension = extension;
}
public String getName() {
return name;
}
/**
* Get the full directory name (rootDirectory is only used if relative == false)
*/
public String getDirectory(String rootDirectory) {
if (relative) {
return rootDirectory + directory;
}
return directory;
}
/**
* Get all the files in the associated directory (not recursively)
* (rootDirectory is only used if relative == false)
*/
StringVector getFiles(String rootDirectory, boolean reload)
throws Exception {
String ext = (this.extension == null ? FileSystemPersistence.DEFAULT_FILE_EXTENSION : this.extension);
String dir = getDirectory(rootDirectory);
XFile xf = new XFile(dir);
if (!xf.exists() || !xf.isDirectory()) {
throw new Exception("Directory '" + dir + "' does not exist");
}
if (files == null || reload) {
files = xf.getFiles(false, "*" + ext);
}
return files;
}
/**
* Creates the associated directory (rootDirectory is only used if relative == false)
*/
public void createDirectory(String rootDirectory)
throws Exception {
String dir = getDirectory(rootDirectory);
XFile file = new XFile(dir);
if (file.exists()) {
if (!file.isDirectory()) {
throw new Exception("Can't create directory '" + dir + "' because this name corresponds to an existing file");
}
} else if (!file.mkdirs()) {
throw new Exception("Can't create directory '" + dir + "'");
}
}
public int getNotSaved(DataModelVector v) {
Iterator iter = needSaving.values().iterator();
int ret = 0;
while (iter.hasNext()) {
DataModel m = (DataModel) iter.next();
v.append(m);
++ret;
}
return ret;
}
public void setNeedSaving(DataModel model) {
needSaving.put(model.getInstanceName(), model);
}
public void removeNeedSaving(String identifier) {
needSaving.remove(identifier);
}
public boolean needSaving(String identifier) {
return needSaving.containsKey(identifier);
}
public void p() {
Iterator keys = needSaving.keySet().iterator();
System.out.print("needSaving[");
while (keys.hasNext()) {
String key = (String) keys.next();
System.out.print(key + " ");
}
System.out.println("]");
}
class ChangeListener implements DataModelListener {
public void dataModelChangeValue(DataModelEvent event) {
setNeedSaving(event.getTransmitter());
}
public void dataModelChangeStructure(DataModelEvent event) {
setNeedSaving(event.getTransmitter());
}
public void dataModelGrabFocus(DataModelEvent event) {
}
}
}