public static QueryMetadataContext buildQueryMetadataContext( final EObject eObject, final boolean restrictSearch) { Collection resources = null; Container container = null; try { // assume model container...modler metadata is for workspace container = ModelerCore.getModelContainer(); ModelWorkspace workspace = ModelerCore.getModelWorkspace(); if (workspace.isOpen()) { resources = Arrays.asList(workspace.getEmfResources()); } else { resources = container.getResources(); } } catch (CoreException e) { TransformationPlugin.Util.log(e); } // find the resoucre for the eObject in the container Resource resource = ModelerCore.getModelEditor().findResource(container, eObject); // create a indexSelector for this resource and instantiate transformation validator ModelResourceIndexSelector selector = new ModelResourceIndexSelector(resource); QueryMetadataContext context = new QueryMetadataContext(selector); // set the resource scope (all model resources in open model projects) context.setResources(resources); // set the restrict search flag context.setRestrictedSearch(restrictSearch); return context; }
/** * Return all metadata records for the entity that matches the given entity name and is of the * type specified by the record type. * * @param recordType * @param entityName the name to match * @param isPartialName true if the entity name is a partially qualified * @throws Exception */ @Override protected Collection findMetadataRecords( final char recordType, final String entityName, final boolean isPartialName) throws Exception { Collection eObjects = new ArrayList(); String uuid = null; if (CoreStringUtil.startsWithIgnoreCase(entityName, UUID.PROTOCOL)) { uuid = entityName.toLowerCase(); } else { String shortName = getShortElementName(entityName); if (CoreStringUtil.startsWithIgnoreCase(shortName, UUID.PROTOCOL)) { uuid = shortName.toLowerCase(); } } // if it the element is a UUID if (uuid != null) { EObject eObj = lookupEObject(uuid); if (eObj != null) { // 12/31/03 (LLP) : fix for 10825. Prevent NPE when column has been deleted. if (eObj.eContainer() != null || eObj.eResource() != null) { eObjects.add(eObj); } } } // no eObjects found, could be cause the name is a "user string" or Eobject for UUID could // not be found in any of open resources if (eObjects.isEmpty()) { Collection sysObjects = findSystemMetadataRecords(recordType, entityName, isPartialName); if (!sysObjects.isEmpty()) { return sysObjects; } // model name is first token List tokens = CoreStringUtil.getTokens(entityName, DELIMITER_STRING); String firstSegment = (String) tokens.get(0); // check if a modelResource exists on the index selector, // if so there are indexes to be queried if (eObjects.isEmpty() && getIndexSelector() instanceof ModelResourceIndexSelector) { if (ModelerCore.DEBUG_QUERY_RESOLUTION) { final String debugMsg = TransformationPlugin.Util.getString( "ModelerMetadata.Resolving_entity_{0}_using_index_files_1", entityName); //$NON-NLS-1$ TransformationPlugin.Util.log(IStatus.INFO, debugMsg); } ModelResourceIndexSelector resourceSelector = (ModelResourceIndexSelector) getIndexSelector(); Object modelResource = ModelerCore.getModelWorkspace().findModelResource(resourceSelector.getResource()); if (modelResource != null) { // look up the index files instead of navigating the resources. Collection records = super.findMetadataRecords(recordType, entityName, isPartialName); if (!super.getContext().isRestrictedSearch() && records.isEmpty()) { // if cant find query all files, there may have been no imports added // some one is trying to resolved a query containing groups for which there are no // imports IndexSelector workspaceSelector = new ModelWorkspaceIndexSelector(); super.getContext().setIndexSelector(workspaceSelector); // look up the index files for the whole workspace instead of looking at imported // resources records = super.findMetadataRecords(recordType, entityName, isPartialName); // set back the resource selector for subsequent metadata lookup super.getContext().setIndexSelector(resourceSelector); } if (ModelerCore.DEBUG_QUERY_RESOLUTION) { final Object[] params = new Object[] {Integer.toString(records.size()), entityName}; final String debugMsg = TransformationPlugin.Util.getString( "ModelerMetadata.Found_{0}_records_for_the_entity_{1}_1", params); //$NON-NLS-1$ ModelerCore.Util.log(IStatus.INFO, debugMsg); } if (!records.isEmpty()) { return records; } if (uuid != null) { ResourceSet[] resourceSets = this.getContainer().getExternalResourceSets(); for (int i = 0; i < resourceSets.length; i++) { ResourceSet currentResourceSet = resourceSets[i]; if (currentResourceSet != null && currentResourceSet instanceof Container) { Container externalContainer = (Container) currentResourceSet; EObject externalEObj = (EObject) externalContainer.getEObjectFinder().find(uuid); if (externalEObj != null) { // 12/31/03 (LLP) : fix for 10825. Prevent NPE when column has been deleted. if (externalEObj.eContainer() != null || externalEObj.eResource() != null) { eObjects.add(externalEObj); } } } } } } } if (ModelerCore.DEBUG_QUERY_RESOLUTION) { final String debugMsg = TransformationPlugin.Util.getString( "ModelerMetadata.Resolving_entity_{0}_by_navigating_the_workspace_1", entityName); //$NON-NLS-1$ TransformationPlugin.Util.log(IStatus.INFO, debugMsg); } // Look up the EObject by path assuming the first path segement // is the model name and the remaining segments are the path within // the model // find all resources for model name Iterator resourceIter = this.findResourcesByName(firstSegment).iterator(); while (resourceIter.hasNext()) { Resource resource = (Resource) resourceIter.next(); // find EObjects in each resource if (resource != null) { Collection entities = findEntitiesByName(resource, entityName, recordType, isPartialName); for (Iterator iter = entities.iterator(); iter.hasNext(); ) { EObject eObj = (EObject) iter.next(); if (eObj != null && (eObj.eContainer() != null || eObj.eResource() != null)) { eObjects.add(eObj); } } } } } // find metadata records for the Eobjects collected if (!eObjects.isEmpty()) { Collection records = createMetadataRecords(recordType, eObjects); if (ModelerCore.DEBUG_QUERY_RESOLUTION) { final Object[] params = new Object[] {Integer.toString(records.size()), entityName}; final String debugMsg = TransformationPlugin.Util.getString( "ModelerMetadata.Found_{0}_records_for_the_entity_{1}_1", params); // $NON-NLS-1$ ModelerCore.Util.log(IStatus.INFO, debugMsg); } return records; } return Collections.EMPTY_LIST; }