Package de.innovationgate.eclipse.utils.ui.model

Source Code of de.innovationgate.eclipse.utils.ui.model.TMLFileMetadataModel

/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*     Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.utils.ui.model;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import de.innovationgate.eclipse.utils.Activator;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
import de.innovationgate.wga.model.AbstractModel;
import de.innovationgate.wga.model.Model;
import de.innovationgate.wga.model.TMLMetadataInfo;
import de.innovationgate.wga.model.ValidationError;
import de.innovationgate.wga.model.WGADesignConfigurationModel;

public class TMLFileMetadataModel extends AbstractModel {

  private IFolder _metadataFolder;
  private IFile _metadataFile;
 
  private boolean directAccess;
  private boolean cacheable;
  private String description;
  private String category;
  private IFile _tmlFile;
  private String _fileEncoding;
 
  private TMLMetadataInfo _defaultInfo = new TMLMetadataInfo();
  private WGADesignConfigurationModel _model;

  public TMLFileMetadataModel(IFile tmlFile) {
    _tmlFile = tmlFile;
   
    WGADesignStructureHelper helper = new WGADesignStructureHelper(_tmlFile);
   
    _fileEncoding = helper.determineDesignEncoding();
   
    try {
      _model = helper.createModel();
    } catch (IOException e) {
      Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, "Unable to retrieve designconfigurationmodel for tml resource '" + _tmlFile.getLocation() + "'.", e));
    }
   
    try {
      reload();
    } catch (IOException e) {
      Activator.getDefault().getLog().log(new Status(Status.ERROR, Activator.PLUGIN_ID, "Unable to init model for tml resource '" + _tmlFile.getLocation() + "'.", e));   
    }
  }

 
  @Override
  public void restoreDefaults() throws IOException {
    setDefaults();
  }

  @Override
  public void reload() throws IOException {
    setDefaults();
   
    // retrieve metadata folder
    _metadataFolder = _tmlFile.getParent().getFolder(new Path("metadata"));
    if (_metadataFolder.exists()) {
      _metadataFile = _metadataFolder.getFile(_tmlFile.getName().substring(0, _tmlFile.getName().length() - _tmlFile.getFileExtension().length()) + "metadata.xml");
      if (_metadataFile.exists()) {
        XStream xstream = new XStream(new DomDriver());
        xstream.alias(TMLMetadataInfo.XSTREAM_ALIAS, TMLMetadataInfo.class);
        Reader reader = null;
        try {
          if(!_metadataFile.isSynchronized(IResource.DEPTH_ZERO)){
            _metadataFile.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
          }
          reader = new InputStreamReader(_metadataFile.getContents(), _fileEncoding);
          TMLMetadataInfo metaData = (TMLMetadataInfo) xstream.fromXML(reader);
          directAccess = metaData.isDirectAccess();
          cacheable = metaData.isCacheable();
          description = metaData.getDescription();
          category = metaData.getCategory();
        } catch (CoreException e) {
          IOException ioe = new IOException("Unable to read metadata file of tml resource '" + _tmlFile.getLocation() + "'.");
          ioe.setStackTrace(e.getStackTrace());
          throw ioe;
        } finally {
          if (reader != null) {
            try {
              reader.close();
            } catch (IOException e) {             
            }
          }
        }
      }
    } 
   
    fireModelChanged();
  }

  @Override
  public void saveChanges() throws IOException {
    if (isDefault()) {
      if (_metadataFile != null && _metadataFile.exists()) {
        try {
          _metadataFile.delete(false, new NullProgressMonitor());
          if (_metadataFolder.members().length == 0) {
              _metadataFolder.delete(false, new NullProgressMonitor());
          }
          reload();
        } catch (CoreException e) {
          IOException ioe = new IOException("Unable to delete meta data file '" + _metadataFile.getLocation() + "'.");
          ioe.setStackTrace(e.getStackTrace());
          throw ioe;
       
      }   
    } else {     
      TMLMetadataInfo metaData = new TMLMetadataInfo();
      metaData.setDirectAccess(directAccess);
      metaData.setDescription(description);
      metaData.setCacheable(cacheable);
      metaData.setCategory(category);
      XStream xstream = new XStream(new DomDriver());
      xstream.alias(TMLMetadataInfo.XSTREAM_ALIAS, TMLMetadataInfo.class);
      Writer writer = null;
      try {
        if (!_metadataFolder.exists()) {
          _metadataFolder.create(true, true, new NullProgressMonitor())
          _metadataFile = _metadataFolder.getFile(_tmlFile.getName().substring(0, _tmlFile.getName().length() - _tmlFile.getFileExtension().length()) + "metadata.xml");
        }

        writer = new OutputStreamWriter(new FileOutputStream(_metadataFile.getLocation().toFile()), _fileEncoding);
        xstream.toXML(metaData, writer);
       
      } catch (CoreException e) {
        IOException ioe = new IOException("Unable to save metadata file '" + _metadataFile.getLocation() + "'.");
        ioe.setStackTrace(e.getStackTrace());
        throw ioe;
      } finally {
        if (writer != null) {
          try {
            writer.close();
          } catch (IOException e) {
          }
          try {
            if(_metadataFile!=null){
              _metadataFile.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
            }
          } catch (CoreException e) {
          }
        }
      }
    }
  }

  @Override
  public List<ValidationError> validate() {
    return new ArrayList<ValidationError>();
  }

  public boolean isDirectAccess() {
    return directAccess;
  }

  public void setDirectAccess(boolean directAccess) {
    this.directAccess = directAccess;
    fireModelChanged();
  }

  public boolean isCachable() {
    return cacheable;
  }

  public void setCachable(boolean cachable) {
    this.cacheable = cachable;
    fireModelChanged();
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
    fireModelChanged();
  }

  public String getCategory() {
    return category;
  }

  public void setCategory(String category) {
    this.category = category;
    fireModelChanged();
  }
 
  private String getCategoryDefault() {
    return _defaultInfo.getCategory();
  }
 
  private String getDescriptionDefault() {
    return _defaultInfo.getDescription();
  }

  private boolean getCacheableDefault() {
    return _defaultInfo.isCacheable();
  }
 
  private boolean getDirectAccessDefault() {
    if (_model != null) {
      return _model.isDirectAccessDefault();
    } else {
      return _defaultInfo.isDirectAccess();
    }
  }
 
 
  private void setDefaults() {   
    cacheable = getCacheableDefault();
    directAccess = getDirectAccessDefault();
    description = getDescriptionDefault();
    category = getCategoryDefault();
    fireModelChanged();
  }
 
  private boolean isDefault() {
    EqualsBuilder builder = new EqualsBuilder();
    builder.append(getDescriptionDefault(), getDescription());
    builder.append(getCategoryDefault(), getCategory());
    builder.append(getDirectAccessDefault(), isDirectAccess());
    builder.append(getCacheableDefault(), isCachable());
    return builder.isEquals();
  }


  public IFile getTmlFile() {
    return _tmlFile;
  }


  @Override
  public boolean isEditable(String propertyName) {
    return true;
  }
}
TOP

Related Classes of de.innovationgate.eclipse.utils.ui.model.TMLFileMetadataModel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.