/*
* SyncTarget.java
*
* Created on April 11, 2007, 8:42 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package org.atomojo.app.db;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Date;
import java.util.logging.Level;
import org.atomojo.app.AtomResource;
import org.atomojo.app.InfosetRepresentation;
import org.atomojo.app.admin.AdminXML;
import org.infoset.xml.Element;
import org.infoset.xml.InfosetFactory;
import org.infoset.xml.XMLException;
import org.infoset.xml.util.XMLWriter;
import org.restlet.data.MediaType;
import org.restlet.representation.FileRepresentation;
import org.restlet.representation.Representation;
/**
*
* @author alex
*/
public class SyncProcess extends XMLObject implements StoredXML
{
String name;
boolean pull;
boolean additive = false;
SyncTarget target;
RemoteApp app;
Date lastSync;
DB db;
/** Creates a new instance of SyncTarget */
public SyncProcess(DB db,Representation rep)
{
super(rep);
this.db = db;
}
public SyncProcess(DB db,String name)
{
super((Element)null);
this.name = name;
this.db = db;
}
public SyncProcess(DB db,String name,boolean pull,SyncTarget target,RemoteApp app,Date lastSync)
{
super((Element)null);
this.name = name;
this.pull = pull;
this.target = target;
this.app = app;
this.lastSync = lastSync;
this.db = db;
}
public SyncProcess(DB db,Element xml)
{
super(xml);
pull = xml.getName().equals(AdminXML.NM_PULL);
name = xml.getAttributeValue("name");
String targetName = xml.getAttributeValue("target");
String remoteName = xml.getAttributeValue("remote");
this.db = db;
this.target = db.getSyncTarget(targetName);
this.app = db.getRemoteApp(remoteName);
}
public String getName() {
return name;
}
public boolean isPullSynchronization() {
return pull;
}
public Date getLastSynchronizedOn() {
return lastSync;
}
public void setLastSynchronizedOn(Date newSync)
{
lastSync = newSync;
}
public SyncTarget getSyncTarget()
{
return target;
}
public RemoteApp getRemoteApp()
{
return app;
}
public boolean isAdditive() {
return additive;
}
public void setAdditive(boolean flag)
{
this.additive = flag;
}
public Representation getRepresentation() {
if (rep==null && name!=null) {
rep = new FileRepresentation(getFile(),MediaType.APPLICATION_XML);
}
return super.getRepresentation();
}
protected File getFile() {
return new File(db.getSyncProcessDir(),name+".xml");
}
public void unmarshall()
throws XMLException
{
if (xml==null) {
try {
parse();
} catch (IOException ex) {
throw new XMLException("Cannot load XML due to I/O exception.",ex);
}
}
pull = xml.getName().equals(AdminXML.NM_PULL);
name = xml.getAttributeValue("name");
String targetName = xml.getAttributeValue("target");
target = new SyncTarget(db,targetName);
String remoteName = xml.getAttributeValue("remote");
app = new RemoteApp(db,remoteName);
String lastSyncValue = xml.getAttributeValue("last-sync");
additive = "true".equals(xml.getAttributeValue("additive"));
if (lastSyncValue!=null) {
try {
lastSync = AtomResource.fromXSDDate(lastSyncValue);
} catch (java.text.ParseException ex) {
throw new XMLException("Cannot parse last-sync date value '"+lastSyncValue+"': "+ex.getMessage(),ex);
}
} else {
lastSync = null;
}
}
public void marshall()
throws XMLException
{
xml = InfosetFactory.getDefaultInfoset().createItemConstructor().createDocument().createDocumentElement(pull ? AdminXML.NM_PULL : AdminXML.NM_PUSH);
xml.setAttributeValue("name",name);
xml.setAttributeValue("target",target.getName());
xml.setAttributeValue("remote",app.getName());
if (additive) {
xml.setAttributeValue("additive","true");
}
if (lastSync!=null) {
xml.setAttributeValue("last-sync",AtomResource.toXSDDate(lastSync));
}
rep = new InfosetRepresentation(MediaType.APPLICATION_XML,xml.getDocument());
}
public boolean exists()
{
return getFile().exists();
}
public boolean create()
{
return update();
}
public boolean delete()
{
return getFile().delete();
}
public boolean update()
{
try {
Writer out = new OutputStreamWriter(new FileOutputStream(getFile()),"UTF-8");
XMLWriter.writeDocument(xml.getDocument(), out);
out.flush();
out.close();
return true;
} catch (Exception ex) {
db.getLogger().log(Level.SEVERE,"Cannot store process to "+getFile()+" due to exception.",ex);
return false;
}
}
}