Package tool

Source Code of tool.ToolProjectSupport

package tool;


import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.QualifiedName;

import tool.model.NameComparator;
import tool.model.ToolPlan;


public class ToolProjectSupport {

  public static final String PROJECT_FOLDER_NAME = "Projects";
  public static final String LIBRARY_FOLDER_NAME = "Libraries";
  public static final String UDS_LIBRARY_FOLDER_NAME = "UDS-Libraries";
  public static final String REPOS_INTERFACE = ".reposInterface";
  public static final String LOCAL_REPOS_FOLDER = ".localRepos";
  public static final String PROJECT_QUALIFIED_NAME = "Tool";

  private static final String REPOS_PROPERTY = "TOOL_REPOS";
  private static final String ROOT_PROPERTY = "TOOL_ROOT";
  private static final String LOGGER_PROPERTY = "TOOL_LOGGER";
  private static final String WORKSPACE_PROPERTY = "TOOL_WORKSPACE";
  private static final String WORKSPACE_PASSWORD_PROPERTY = "TOOL_WORKSPACE_PASSWORD";

  public static final String REPOS_OBJECT = "TOOL_REPOS_OBJECT";
  public static final String REPOS_SESSION_OBJECT = "TOOL_REPOS_SESSION_OBJECT";
  //  public static final String CLASS_CACHE = "TOOL CLASS CACHE";
  public static final String PLAN_CACHE = "TOOL PLAN CACHE";
  public static final String CURSOR_CACHE = "TOOL CURSOR CACHE";
  public static final String SO_CACHE = "TOOL SERVICE OBJECT CACHE";
  //  public static final String INTERFACE_CACHE = "TOOL INTERFACE CACHE";
  public static final String TYPE_CACHE = "TOOL TYPE CACHE";

  public static final String DEFAULT_LOG_FLAGS = "%stdout(err:sh:* cfg:c4:23:* cfg:os:21 trc:ui:10:1)";

  private static final String TOOL_PLAN_PROPERTY = "TOOL PLAN PROPERTY";
  public static final QualifiedName toolPlanQualifiedName = new QualifiedName(PROJECT_QUALIFIED_NAME, TOOL_PLAN_PROPERTY);
  public static final QualifiedName worspacePasswordQualifiedName = new QualifiedName(PROJECT_QUALIFIED_NAME, WORKSPACE_PASSWORD_PROPERTY);
  private static final String SOURCE_FOLDER = "SourceFolder";
  public static final QualifiedName sourceFolderQualifiedName = new QualifiedName(PROJECT_QUALIFIED_NAME, SOURCE_FOLDER);
  public static final QualifiedName forteRootQualifiedName = new QualifiedName(PROJECT_QUALIFIED_NAME, ROOT_PROPERTY);
  public static final QualifiedName loggerQualifiedName = new QualifiedName(PROJECT_QUALIFIED_NAME, LOGGER_PROPERTY);
  public static final QualifiedName reposQualifiedName = new QualifiedName(PROJECT_QUALIFIED_NAME, REPOS_PROPERTY);
  public static final QualifiedName workspaceQualifiedName = new QualifiedName(PROJECT_QUALIFIED_NAME, WORKSPACE_PROPERTY);
  private static final String TOOL_PLAN_NAME = "ToolPlanName";
  public static final QualifiedName toolPlanNameQualifiedName = new QualifiedName(PROJECT_QUALIFIED_NAME, TOOL_PLAN_NAME);
  public static final String[] forteLibrariesList = new String[] {
    "APPC",
    "Configuration",
    "Corba",
    "CosNaming",
    "DB2",
    "DCE",
    "DDEProject",
    "DisplayProject",
    "EnvManagement",
    "ForeignObjMgr",
    "Framework",
    "GenericDBMS",
    "GenericRepository",
    "HTTP",
    "HTTPSupport",
    "Informix",
    "Ingres",
    "LDAP",
    "ODBC",
    "OLE",
    "OLE2",
    "Oracle",
    "Oracle7",
    "Rdb",
    "Sybase",
    "SybaseCTLib",
    "SystemMonitor",
    "TOOLCompiler",
    "TOOLInterpreter",
    "XMLDOM2",
    "XMLParser",
    "XMLSAX2",
    "XMLSvr",
    "XSLT"
  };
  public static final String FORTE_LIBRARY_CACHE = "UDSLibrary";
  public static Set<String> forteLibrariesSet = new HashSet<String>();
  static {
    for (String libName : forteLibrariesList){
      forteLibrariesSet.add(libName);
    }
  }
  private ToolProjectSupport(){

  }
  public static IProject createProject(String projectName, URI location,
      String logFlags, String repository, String workspace, String workspacePassword) throws CoreException, IOException {
    Assert.isNotNull(projectName);
    Assert.isTrue(projectName.trim().length() > 0);

    IProject project = createBaseProject(projectName, location);
    //            addNature(project);
    project.setPersistentProperty(ToolProjectSupport.forteRootQualifiedName, getForteRoot());
    project.setPersistentProperty(loggerQualifiedName, logFlags);
    project.setPersistentProperty(reposQualifiedName, repository);
    project.setPersistentProperty(workspaceQualifiedName, workspace);
    project.setPersistentProperty(worspacePasswordQualifiedName, workspacePassword);

    String[] paths = {
        PROJECT_FOLDER_NAME,
        LIBRARY_FOLDER_NAME,
        UDS_LIBRARY_FOLDER_NAME,
        REPOS_INTERFACE,
        LOCAL_REPOS_FOLDER};
    addToProjectStructure(project, paths);
    //copyImageRepositories(project);



    return project;
  }

  //    public static void importFromRepository(IProject project, IWizardContainer container){
  //      try {
  //      if (container == null){
  //        IWorkbench wb = PlatformUI.getWorkbench();
  //        IProgressService ps = wb.getProgressService();
  //        ps.run(false, true, new RepositoryExport(project));
  //      } else {
  //        container.run(false, true, new RepositoryExport(project));
  //      }
  //    } catch (InvocationTargetException e) {
  //      ToolModelActivator.showError("Error importing from Tool Repository",e);
  //    } catch (InterruptedException e) {
  //      ToolModelActivator.showError("Error importing from Tool Repository",e);
  //    }
  //    }

  //    @Deprecated
  //  public static void copyForteLibraries(IProject project) throws IOException {
  //    IFolder udsFolder = (IFolder)project.findMember(UDS_LIBRARY_FOLDER_NAME);
  //    File forteLibsDir = udsFolder.getLocation().toFile();
  //    if (forteLibsDir.exists()) {
  //      URL url = Platform.getBundle(ToolModelActivator.PLUGIN_ID).getEntry("UDSLibraries");
  //      File udsLibraries;
  //      try {
  //        udsLibraries = new File(FileLocator.toFileURL(url).toURI());
  //        File[] files = udsLibraries.listFiles(new FileFilter() {
  //         
  //          @Override
  //          public boolean accept(File pathname) {
  //            if (pathname.getName().endsWith(".pex"))
  //                  return true;
  //            return false;
  //          }
  //        });
  //        if (files != null){
  //          for (File fOrig : files){
  //            String dest = fOrig.getName();
  //            File fDest = new File(forteLibsDir, dest);
  //            ToolModelActivator.copyFile(fOrig, fDest);
  //          }
  //          udsFolder.refreshLocal(IResource.DEPTH_ONE, null);
  //        }
  //      } catch (URISyntaxException e) {
  //        ToolModelActivator.showError("Error copying Tool libraries", e);
  //      } catch (CoreException e) {
  //        ToolModelActivator.showError("Error copying Tool libraries", e);
  //      }
  //        }
  //  }

  //  /**
  //   * this method copies the repository interface and formeditor image repository
  //   * from the plugin to a hidden directory in the project.
  //   *
  //   * This means each project has a copy of the interface image repository and the formeditor image repository.
  //   *
  //   * @param project
  //   * @throws IOException
  //   */
  //    @Deprecated
  //  public static void copyImageRepositories(IProject project) throws IOException {
  //    IFolder reposInterfaceFolder = (IFolder)project.findMember(REPOS_INTERFACE);
  //    File reposInterfaceDir = reposInterfaceFolder.getLocation().toFile();
  //    if (reposInterfaceDir.exists()) {
  //      File reposDir;
  //      try {
  //        reposDir = getInterfaceDir();
  //        File[] files = reposDir.listFiles(new FileFilter() {
  //         
  //          @Override
  //          public boolean accept(File pathname) {
  //            if (pathname.getName().startsWith("reposi0") || pathname.getName().startsWith("formed0"))
  //                  return true;
  //            return false;
  //          }
  //        });
  //        if (files != null){
  //          for (File fOrig : files){
  //            String dest = fOrig.getName();
  //            File fDest = new File(reposInterfaceDir, dest);
  //            ToolModelActivator.copyFile(fOrig, fDest);
  //          }
  //        }
  //      } catch (URISyntaxException e) {
  //        ToolModelActivator.showError("Error copying Repository Interface", e);
  //      }
  //        }
  //  }

  public static File getInterfaceDir() throws URISyntaxException, IOException{
    URL url = Platform.getBundle(ToolModelActivator.PLUGIN_ID).getEntry("ReposInterface");
    File reposDir = new File(FileLocator.toFileURL(url).toURI());
    return reposDir;
  }

  public static String getForteRoot(){
    URL url = Platform.getBundle(ToolModelActivator.PLUGIN_ID).getEntry("Forte");
    File rootDir = null;
    try {
      rootDir = new File(FileLocator.toFileURL(url).toURI());
    } catch (URISyntaxException e) {
      ToolModelActivator.showError("Cannot get FORTE_ROOT", e);
    } catch (IOException e) {
      ToolModelActivator.showError("Cannot get FORTE_ROOT", e);
    }
    return (rootDir == null) ? "" : rootDir.getAbsolutePath();
  }

  /**
   * Just do the basics: create a basic project.
   *
   * @param location
   * @param projectName
   */
  private static IProject createBaseProject(String projectName, URI location) {
    // it is acceptable to use the ResourcesPlugin class
    IProject newProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);

    if (!newProject.exists()) {
      URI projectLocation = location;
      IProjectDescription desc = newProject.getWorkspace().newProjectDescription(newProject.getName());
      if (location != null && ResourcesPlugin.getWorkspace().getRoot().getLocationURI().equals(location)) {
        projectLocation = null;
      }

      desc.setLocationURI(projectLocation);
      try {

        newProject.create(desc, new NullProgressMonitor());
        if (!newProject.isOpen()) {
          newProject.open(new NullProgressMonitor());
        }
      } catch (CoreException e) {
        ToolModelActivator.showError("Error creating base project.", e);
      }
    }

    return newProject;
  }

  private static void createFolder(IFolder folder) throws CoreException {
    IContainer parent = folder.getParent();
    if (parent instanceof IFolder) {
      createFolder((IFolder) parent);
    }
    if (!folder.exists()) {
      folder.create(false, true, null);
    }
  }

  public static void createSourceFolder(IFolder folder) throws CoreException {
    IContainer parent = folder.getParent();
    if (parent instanceof IFolder) {
      createFolder((IFolder) parent);
    }
    if (!folder.exists())
      folder.create(IResource.FORCE, true, null);
    setSourceFolder(folder);
  }

  public static IFile createNewPlan(IFolder sourceFolder, String planName, boolean display, boolean dataBase) throws CoreException {
    if (!sourceFolder.exists())
      return null;
    if (!isSourceFolder(sourceFolder))
      return null;

    IFile prxFile = null;

    IFolder newPlanFolder = sourceFolder.getFolder(planName);
    newPlanFolder.create(IResource.FORCE, true, null);
    newPlanFolder.setPersistentProperty(toolPlanNameQualifiedName, planName);

    ToolPlan newToolPlan = ToolPlan.newPlan(newPlanFolder, planName, display, dataBase);

    prxFile = newToolPlan.getFile();
    return prxFile;
  }
  /**
   * Create a folder structure with a parent root, overlay, and a few child
   * folders.
   *
   * @param newProject
   * @param paths
   * @throws CoreException
   */
  private static void addToProjectStructure(IProject newProject, String[] paths) throws CoreException {
    for (String path : paths) {
      IFolder etcFolders = newProject.getFolder(path);
      if (path.endsWith(PROJECT_FOLDER_NAME))
        createSourceFolder(etcFolders);
      else
        createFolder(etcFolders);
    }
  }

  //    private static void addNature(IProject project) throws CoreException {
  //        if (!project.hasNature(ToolNature.NATURE_ID)) {
  //            IProjectDescription description = project.getDescription();
  //            String[] prevNatures = description.getNatureIds();
  //            String[] newNatures = new String[prevNatures.length + 1];
  //            System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
  //            newNatures[prevNatures.length] = ToolNature.NATURE_ID;
  //            description.setNatureIds(newNatures);
  //
  //            IProgressMonitor monitor = null;
  //            project.setDescription(description, monitor);
  //        }
  //    }

  public static IFolder getLocalReposFolders(IProject project){
    IFolder localRepos = (IFolder) project.findMember(LOCAL_REPOS_FOLDER);
    return localRepos;

  }

  public static List<IFolder> getSourceFolders(IProject project){
    List<IFolder> sourceFolders = new ArrayList<IFolder>();
    try {
      IResource[] members = project.members();
      for (IResource member : members){
        if (member instanceof IFolder){
          if (isSourceFolder(member))
            sourceFolders.add((IFolder)member);
        }
      }
    } catch (CoreException e) {
      ToolModelActivator.showError("Error geting source folders", e);
    }

    return sourceFolders;
  }

  public static boolean isSourceFolder(IResource resource){
    if (!(resource instanceof IFolder))
      return false;
    IFolder folder = (IFolder)resource;
    String sourceFolder;
    try {
      sourceFolder = folder.getPersistentProperty(ToolProjectSupport.sourceFolderQualifiedName);
    if (sourceFolder != null && sourceFolder.equalsIgnoreCase("true"))
      return true;
    } catch (CoreException e) {
      ToolModelActivator.showError("Decorator Error", e);
    }
    return false;
  }

  public static void setSourceFolder(IFolder folder){
    try {
      folder.setPersistentProperty(sourceFolderQualifiedName, "true");
    } catch (CoreException e) {
      ToolModelActivator.showError("Error checking source folder", e);
    }
  }

  public static boolean isPlanFolder(IFolder planDir) {
    String planName;
    try {
      planName = planDir.getPersistentProperty(toolPlanNameQualifiedName);
      if (planName != null){
        return true;
      } else {
        ToolPlan plan = addPlanToFolder(planName, planDir);
        return (plan != null);
      }
    } catch (CoreException e) {
      ToolModelActivator.showError("Error checking Tool plan folder", e);
    }
    return false;
  }

  /**
   * returns the ToolPlan from a folder.
   * returns null if it is not a plan folder
   * @param planDir
   * @return
   */
  public static ToolPlan getPlanFromFolder(IFolder planDir) {
    if (!isPlanFolder(planDir))
      return null;
    try {
      ToolPlan plan = (ToolPlan)planDir.getSessionProperty(toolPlanQualifiedName);
      if (plan == null)
        plan = addPlanToFolder(planDir.getName(), planDir);
      return plan;
    } catch (CoreException e) {
      ToolModelActivator.showError("Error getting Plan from folder", e);
    }
    return null;
  }

  public static List<ToolPlan> getPlansfromSourceFolder(IFolder sourceFolder){
    if (!isSourceFolder(sourceFolder)){
      return null;
    }
    List<ToolPlan> list = new ArrayList<ToolPlan>();
    try {
      IResource[] members = sourceFolder.members();
      for (IResource member : members){
        if (member instanceof IFolder){
          if (isPlanFolder((IFolder)member))
            list.add(getPlanFromFolder((IFolder)member));
        }
      }
    } catch (CoreException e) {
      ToolModelActivator.showError("Error getting Plans", e);
    }

    return list;
  }

  public static Set<ToolPlan> getAllPlansForProject(IProject project){
    Set<ToolPlan> list = new TreeSet<ToolPlan>(new NameComparator());
    List<IFolder> sourceFolders = ToolProjectSupport.getSourceFolders(project);
    for (IFolder sourceFolder : sourceFolders){
      list.addAll(getPlansfromSourceFolder(sourceFolder));
    }
//    for (ToolPlan plan : ToolPlan.getForteLibraryCache(project).values()){
//      list.add(plan);
//    }
    return list;
  }


  public static ToolPlan addPlanToFolder(String planName, IFolder planDir) throws CoreException{
    IFile prxFile = (IFile)planDir.findMember(planDir.getName() + ".prx");
    if (prxFile != null){
      ToolPlan plan = ToolPlan.getInstance(prxFile);
      planDir.setPersistentProperty(toolPlanNameQualifiedName, planName);
      planDir.setSessionProperty(toolPlanQualifiedName, plan);
      return plan;
    }
    return null;
  }

  public static void removePlanFromFolder(IFolder planDir) throws CoreException{
    planDir.setPersistentProperty(toolPlanNameQualifiedName, null);
  }

  public static String getForteRoot(IProject project) {
    String forteRoot = null;
    try {
      forteRoot = project.getPersistentProperty(ToolProjectSupport.forteRootQualifiedName);
    } catch (CoreException e) {
      ToolModelActivator.showError("Error getting FORTE_ROOT from persistence store",e);
    }
    return forteRoot;
  }
  public static String getLogFlags(IProject project) {
    String logFlags = null;
    try {
      logFlags = project.getPersistentProperty(ToolProjectSupport.loggerQualifiedName);
    } catch (CoreException e) {
      ToolModelActivator.showError("Error getting Log Flags from persistence store",e);
    }
    return logFlags;
  }
  public static String getRepositoryName(IProject project) {
    String reposName = null;
    try {
      reposName = project.getPersistentProperty(ToolProjectSupport.reposQualifiedName);
    } catch (CoreException e) {
      ToolModelActivator.showError("Error getting Repository Name from persistence store",e);
    }
    return reposName;
  }
  public static boolean isRunnablePlanFolder(IFolder resource) {
    ToolPlan plan = getPlanFromFolder(resource);
    if (plan == null)
      return false;
    return ((plan.getStartMethod() != null && !plan.getStartMethod().isEmpty()) &&
        (plan.getStartClass() != null && !plan.getStartClass().isEmpty()));
  }
  public static IFolder getForteLibraryFolder(IProject project) {
    IFolder folder = project.getFolder(UDS_LIBRARY_FOLDER_NAME);
    return folder;
  }


  public static IFolder getLibraryFolder(IProject project) {
    IFolder folder = project.getFolder(LIBRARY_FOLDER_NAME);
    return folder;
  }

  public static void copyForteLibraries(IProject project) {
    IFolder udsFolder = project.getFolder(UDS_LIBRARY_FOLDER_NAME);
//    if (udsFolder.exists())
//      return;
    try {
      if (!udsFolder.exists()){
        udsFolder.create(true, true, null);
       
      }

      File forteLibsDir = udsFolder.getLocation().toFile();
      if (forteLibsDir.exists()) {
        URL url = Platform.getBundle(ToolModelActivator.PLUGIN_ID).getEntry("UDSLibraries");
        File udsLibraries;
        udsLibraries = new File(FileLocator.toFileURL(url).toURI());
        File[] files = udsLibraries.listFiles(new FileFilter() {

          @Override
          public boolean accept(File pathname) {
            if (pathname.getName().endsWith(".pex"))
              return true;
            return false;
          }
        });
        if (files != null){
          for (File fOrig : files){
            IFile newFile = udsFolder.getFile(new Path(fOrig.getName()));
            if (!newFile.exists()){
              newFile.create(new FileInputStream(fOrig), true, null);
            }
          }
        }
      }
      project.refreshLocal(IResource.DEPTH_ONE, null);
    } catch (URISyntaxException e) {
      ToolModelActivator.showError("Error copying Tool libraries", e);
    } catch (CoreException e) {
      ToolModelActivator.showError("Error copying Tool libraries", e);
    } catch (FileNotFoundException e) {
      ToolModelActivator.showError("Error copying Tool libraries", e);
    } catch (IOException e) {
      ToolModelActivator.showError("Error copying Tool libraries", e);
    }
  }

}
TOP

Related Classes of tool.ToolProjectSupport

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.