/*******************************************************************************
* Copyright (c) 2006-2012
* Software Technology Group, Dresden University of Technology
* DevBoost GmbH, Berlin, Amtsgericht Charlottenburg, HRB 140026
*
* 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:
* Software Technology Group - TU Dresden, Germany;
* DevBoost GmbH - Berlin, Germany
* - initial API and implementation
******************************************************************************/
package org.reuseware.sokan.index.indexer;
import java.util.List;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.reuseware.sokan.ID;
import org.reuseware.sokan.IndexMetaData;
import org.reuseware.sokan.IndexRow;
import org.reuseware.sokan.index.util.ResourceUtil;
import org.reuseware.sokan.index.util.RowUtil;
/**
* The meta data manager is responsible for creating index rows
* by calling indexers.
*/
public class MetaDataManager {
private List<Indexer> indexers;
/**
* Constructs a meta data manager for the given list of indexers.
*
* @param indexers the indexers this meta data manager can call
*/
public MetaDataManager(List<Indexer> indexers) {
this.indexers = indexers;
}
/**
* @param uri URI of the artifact to index
* @param generated true if the resource was generated by an indexer
* @param indexersToUse a subset of indexers to use in this indexing phase
* @param oldMetaData the previous index row of the artifact
* @param resourceSet the resource set to use
* @param monitor a monitor to record progress
* @return the created index row
*/
public IndexRow createIndexRow(URI uri, boolean generated, List<Indexer> indexersToUse,
IndexMetaData oldMetaData, ResourceSet resourceSet, IProgressMonitor monitor) {
if (uri == null || indexersToUse == null) {
return null;
}
return build(uri, generated, indexersToUse, oldMetaData, resourceSet, monitor);
}
/**
* @param resource resource representing the artifact to index
* @param generated true if the resource was generated by an indexer
* @param indexersToUse a subset of indexers to use in this indexing phase
* @param oldMetaData the previous index row of the artifact
* @param resourceSet the resource set to use
* @param monitor a monitor to record progress
* @return the created index row
*/
public IndexRow createIndexRow(IResource resource, boolean generated,
List<Indexer> indexersToUse, IndexMetaData oldMetaData,
ResourceSet resourceSet, IProgressMonitor monitor) {
URI physicalURI = ResourceUtil.uriFrom(resource);
return build(physicalURI, generated, indexersToUse, oldMetaData, resourceSet, monitor);
}
private IndexRow build(URI physicalURI, boolean generated, List<Indexer> indexersToUse,
IndexMetaData oldMetaData, ResourceSet resourceSet, IProgressMonitor monitor) {
ID artifactID = ResourceUtil.idFrom(physicalURI);
if (artifactID == null) {
return null;
}
String idString = ResourceUtil.idString(artifactID);
String uriString = ResourceUtil.uriString(physicalURI);
IndexMetaData myData;
if (oldMetaData != null) {
myData = oldMetaData;
} else {
myData = RowUtil.buildMetaData();
}
invokeRegisteredIndexers(physicalURI, myData,
indexersToUse, resourceSet, monitor);
return RowUtil.buildIndexRow(idString, uriString, myData, generated);
}
private void invokeRegisteredIndexers(URI artifactURI, IndexMetaData data,
List<Indexer> indexersToUse, ResourceSet resourceSet, IProgressMonitor monitor) {
IndexMetaData metaData = RowUtil.buildMetaData();
if (resourceSet == null) {
return;
}
List<Indexer> myIndexers;
if (indexersToUse == null) {
myIndexers = indexers;
} else {
myIndexers = indexersToUse;
}
for (Indexer indexer : myIndexers) {
String statusMessage = "Indexing " + artifactURI.toPlatformString(true) + " (" + indexer.getClass().getSimpleName() + ")";
monitor.subTask(statusMessage);
//System.out.println(statusMessage);
indexer.createIndex(artifactURI, data, resourceSet);
monitor.worked(1);
if (data.isEmpty()) {
continue;
}
metaData.putAll(data);
}
}
}