Skip to content

Commit

Permalink
TEIIDDES-1697: Replace project loop-throughs with utility methods
Browse files Browse the repository at this point in the history
* DotProjectUtils provides a helpful method for returning the collection of
  open modelling projects. Instead of code iterating through all projects
  and determining the same result, delegate to the utility method.

* DotProjectUtils
 * Throw single Exception rather than the singular versions as it then
   avoids the need for client code to depend on the jdom plugin.
 * Migrate hasNature method from ModelerCore since this is a better logical
   home for it.

* [Fake]ModelWorkspace[Impl,Info]
 * Remove the getNonModellingResources method since it is performant
   expensive and ultimately pointless
  • Loading branch information
Paul Richardson committed May 2, 2013
1 parent 948a688 commit 5e279e2
Show file tree
Hide file tree
Showing 16 changed files with 100 additions and 244 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import org.teiid.designer.core.util.StartupLogger;
import org.teiid.designer.core.util.WorkspaceUriPathConverter;
import org.teiid.designer.core.validation.ValidationRuleManager;
import org.teiid.designer.core.workspace.DotProjectUtils;
import org.teiid.designer.core.workspace.ModelProject;
import org.teiid.designer.core.workspace.ModelResource;
import org.teiid.designer.core.workspace.ModelStatusImpl;
Expand Down Expand Up @@ -1299,30 +1300,7 @@ public static DatatypeManager getWorkspaceDatatypeManager() {
* @since 4.0
*/
public static boolean hasModelNature( final IProject project ) {
CoreArgCheck.isNotNull(project);
try {
return project.hasNature(NATURE_ID);
} catch (final CoreException e) {
// project does not exist or is not open
}
return false;
}

/**
* Returns true if the given project is accessible and it has a java nature, otherwise false.
*
* @since 4.0
*/
public static boolean hasNature( final IProject project,
final String nature ) {
CoreArgCheck.isNotNull(project);
CoreArgCheck.isNotEmpty(nature);
try {
return project.hasNature(nature);
} catch (final CoreException e) {
// project does not exist or is not open
}
return false;
return DotProjectUtils.isModelerProject(project);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.teiid.core.designer.util.CoreArgCheck;
import org.teiid.designer.common.xml.JdomHelper;
import org.teiid.designer.core.ModelerCore;

Expand Down Expand Up @@ -69,7 +69,7 @@ public static IFile getDotProjectFile( IContainer container ) {

public static int getDotProjectCount( String fileName,
boolean recurse,
boolean onlyModelerProjects ) throws IOException, JDOMException {
boolean onlyModelerProjects ) throws Exception {
File file = new File(fileName);
return getDotProjectCount(file, recurse, onlyModelerProjects);
}
Expand All @@ -86,7 +86,7 @@ public static int getDotProjectCount( String fileName,
*/
public static int getDotProjectCount( File file,
boolean recurse,
boolean onlyModelerProjects ) throws IOException, JDOMException {
boolean onlyModelerProjects ) throws Exception {
int dotProjectCount = 0;
int depth = 0;
List<File> resources = new ArrayList<File>();
Expand Down Expand Up @@ -122,7 +122,7 @@ public static int getDotProjectCount( File file,
*/

public static boolean isDotProject( String file,
boolean onlyModelerProject ) throws IOException, JDOMException {
boolean onlyModelerProject ) throws Exception {
return isDotProject(new File(file), onlyModelerProject);
}

Expand All @@ -135,7 +135,7 @@ public static boolean isDotProject( String file,
* @return
*/
public static boolean isDotProject( File file,
boolean onlyModelerProject ) throws IOException, JDOMException {
boolean onlyModelerProject ) throws Exception {
if (file.getName().equals(DOT_PROJECT)) {
if (onlyModelerProject) {
Document doc = JdomHelper.buildDocument(file);
Expand Down Expand Up @@ -165,7 +165,7 @@ public static boolean isDotProject( File file,
*/
public static int getDotProjectCount( IResource targetResource,
boolean recurse,
boolean onlyModelerProjects ) throws CoreException, IOException, JDOMException {
boolean onlyModelerProjects ) throws Exception {
int dotProjectCount = 0;
int depth = 0;
List<IResource> resources = new ArrayList<IResource>();
Expand Down Expand Up @@ -201,13 +201,12 @@ public static int getDotProjectCount( IResource targetResource,
* @return
*/
public static boolean isDotProject( IResource resource,
boolean onlyModelerProject ) throws CoreException, IOException, JDOMException {
boolean onlyModelerProject ) throws Exception {
if (resource.getName().equals(DOT_PROJECT) && resource.getType() == IResource.FILE) {
if (onlyModelerProject) {
if (resource.isAccessible()) {
if (resource.getProject().isOpen()) {
IProjectNature nature = resource.getProject().getNature(ModelerCore.NATURE_ID);
return nature != null;
return resource.getProject().hasNature(ModelerCore.NATURE_ID);
}

return isModelNature(resource);
Expand Down Expand Up @@ -291,30 +290,38 @@ public static boolean isModelerProject( IProject iProject ) {
return result;
}

/**
* Returns true if the given project is accessible and it has a java nature, otherwise false.
*
* @since 4.0
*/
public static boolean hasNature( final IProject project, final String nature ) {
CoreArgCheck.isNotNull(project);
CoreArgCheck.isNotEmpty(nature);
try {
return project.hasNature(nature);
} catch (final CoreException e) {
// project does not exist or is not open
}
return false;
}

/**
* Returns a list of open Model Projects within the workspace
*
* @return array of Modeler <code>IProject</code>s
* @return collection of Modeler <code>IProject</code>s
*/
public static IProject[] getOpenModelProjects() {
public static Collection<IProject> getOpenModelProjects() {
IProject[] allProjects = ModelerCore.getWorkspace().getRoot().getProjects();

List<IProject> openModelProjectList = new ArrayList<IProject>(allProjects.length);

for (IProject proj : allProjects) {
if (proj.isOpen() && isModelerProject(proj) && !ModelerCore.hasNature(proj, ModelerCore.HIDDEN_PROJECT_NATURE_ID)) {
if (proj.isOpen() && isModelerProject(proj) && !hasNature(proj, ModelerCore.HIDDEN_PROJECT_NATURE_ID)) {
openModelProjectList.add(proj);
}
}

IProject[] projArray = new IProject[openModelProjectList.size()];
int i = 0;
for (IProject mProj : openModelProjectList) {
projArray[i++] = mProj;
}

return projArray;
return openModelProjectList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,6 @@ ModelProject createModelProject(String name, IPath path, IProgressMonitor monito
* @exception ModelWorkspaceException if this request fails.
*/
ModelProject[] getModelProjects() throws ModelWorkspaceException;

/**
* Returns an array of non-model resources (that is, non-Modeling projects) in
* the workspace.
* <p>
* Non-Modeling projects include all projects that are closed (even if they have the
* Modeling nature).
* </p>
*
* @return an array of non-modeling projects contained in the workspace.
* @throws ModelWorkspaceException if this element does not exist or if an
* exception occurs while accessing its corresponding resource
*/
Object[] getNonModelingResources() throws ModelWorkspaceException;

/**
* Obtains all the {@link ModelResource}s found in open model projects in the workspace.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
Expand Down Expand Up @@ -151,13 +151,11 @@ protected boolean generateInfos( final OpenableModelWorkspaceItemInfo info,
final IResource underlyingResource ) {

ModelWorkspaceManager.getModelWorkspaceManager().putInfo(this, info);

// determine my children
IProject[] projects = ModelerCore.getWorkspace().getRoot().getProjects();
for (int i = 0, max = projects.length; i < max; i++) {
IProject project = projects[i];
if (ModelerCore.hasModelNature(project)) {
info.addChild(getModelProject(project));
}
Collection<IProject> projects = DotProjectUtils.getOpenModelProjects();
for (IProject project : projects) {
info.addChild(getModelProject(project));
}
return true;
}
Expand Down Expand Up @@ -499,14 +497,6 @@ public Resource[] getEmfResources() throws CoreException {
return result;
}

/**
* @see ModelWorkspace
*/
@Override
public Object[] getNonModelingResources() throws ModelWorkspaceException {
return ((ModelWorkspaceInfo)getItemInfo()).getNonModelResources();
}

/* (non-Javadoc)
* @see org.teiid.designer.core.workspace.OpenableImpl#createItemInfo()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*/
package org.teiid.designer.core.workspace;

import org.eclipse.core.resources.IProject;
import org.teiid.designer.core.ModelerCore;

/**
* ModelWorkspaceInfo
Expand All @@ -17,51 +15,9 @@
*/
public class ModelWorkspaceInfo extends OpenableModelWorkspaceItemInfo {

/**
* A array with all the non-model projects contained by this model
*/
Object[] nonModelResources;

/**
* Constructs a new Model Workspace Info
*/
protected ModelWorkspaceInfo() {
}
/**
* Compute the non-java resources contained in this java project.
*/
private Object[] computeNonModelResources() {
IProject[] projects = ModelerCore.getWorkspace().getRoot().getProjects();
int length = projects.length;
Object[] nonModelResourcesTemp = null;
int index = 0;
for (int i = 0; i < length; i++) {
IProject project = projects[i];
if (!ModelerCore.hasModelNature(project)) {
if (nonModelResourcesTemp == null) {
nonModelResourcesTemp = new Object[length];
}
nonModelResourcesTemp[index++] = project;
}
}
if (index == 0) return NO_NON_MODEL_RESOURCES;
if (index < length) {
System.arraycopy(nonModelResources, 0, nonModelResources = new Object[index], 0, index);
}
return nonModelResourcesTemp;
}

/**
* Returns an array of non-java resources contained in the receiver.
*/
Object[] getNonModelResources() {

Object[] nonModelResources = this.nonModelResources;
if (nonModelResources == null) {
nonModelResources = computeNonModelResources();
this.nonModelResources = nonModelResources;
}
return nonModelResources;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.eclipse.core.resources.IProject;
Expand Down Expand Up @@ -124,10 +125,11 @@ public void init( final IWorkbench workbench,
finalSelection = new StructuredSelection(newProject);
}
}
final IProject[] projects = DotProjectUtils.getOpenModelProjects();
final Collection<IProject> projects = DotProjectUtils.getOpenModelProjects();
IProject[] projectArray = projects.toArray(new IProject[0]);

importer = new DdlImporter(projects);
srcPg = new DdlImporterPage(importer, projects, finalSelection);
importer = new DdlImporter(projectArray);
srcPg = new DdlImporterPage(importer, projectArray, finalSelection);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.teiid.designer.core.metamodel.MetamodelDescriptor;
import org.teiid.designer.core.util.CoreModelObjectNotificationHelper;
import org.teiid.designer.core.util.StringUtilities;
import org.teiid.designer.core.workspace.DotProjectUtils;
import org.teiid.designer.core.workspace.ModelFileUtil;
import org.teiid.designer.core.workspace.ModelResource;
import org.teiid.designer.core.workspace.ModelUtil;
Expand Down Expand Up @@ -641,10 +642,6 @@ private void findPvdbs( IContainer container,
}
}

private IProject[] getAllProjects() {
return ModelerCore.getWorkspace().getRoot().getProjects();
}

private IFile getFile( IPath path ) {
return ModelerCore.getWorkspace().getRoot().getFile(path);
}
Expand Down Expand Up @@ -1575,7 +1572,7 @@ private Collection<Job> rebuildPreviewVdbs() {
/*
* Loop through all the projects and find their preview vdbs
*/
for (IProject project : getAllProjects()) {
for (IProject project : DotProjectUtils.getOpenModelProjects()) {
List<IPath> pvdbPaths = new ArrayList<IPath>();
try {
if (project.isOpen() && ModelerCore.hasModelNature(project)) {
Expand Down Expand Up @@ -1665,7 +1662,7 @@ public String getPreviewVdbJndiName( IPath pvdbPath ) {
};

// delete all Preview VDBs on old server
for (IProject project : getAllProjects()) {
for (IProject project : DotProjectUtils.getOpenModelProjects()) {
for (IFile pvdbFile : findProjectPvdbs(project, false)) {
Job deleteDeployedPvdbJob = new DeleteDeployedPreviewVdbJob(getPreviewVdbDeployedName(pvdbFile),
getPreviewVdbVersion(pvdbFile),
Expand Down Expand Up @@ -1775,7 +1772,7 @@ private void startup() throws Exception {

// when Eclipse starts you don't get an open project event so pretend we did get one and call the event handler
if (this.previewEnabled) {
for (IProject project : getAllProjects()) {
for (IProject project : DotProjectUtils.getOpenModelProjects()) {
if (project.isOpen() && (ModelerCore.hasModelNature(project))) {
modelProjectOpened(project);
}
Expand All @@ -1787,10 +1784,8 @@ private void startup() throws Exception {
* Make sure all PVDBs exist and are synchronized.
*/
private void synchronizeWorkspace() {
for (IProject project : getAllProjects()) {
if (project.isOpen() && (ModelerCore.hasModelNature(project))) {
modelProjectOpened(project);
}
for (IProject project : DotProjectUtils.getOpenModelProjects()) {
modelProjectOpened(project);
}
}

Expand Down

0 comments on commit 5e279e2

Please sign in to comment.