blob: bf3bee8437e8f9347828bef5c471c1baf4a7776e [file] [log] [blame]
//------------------------------------------------------------------------------
// Copyright (c) 2005, 2006 IBM Corporation and others.
// 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:
// IBM Corporation - initial implementation
//------------------------------------------------------------------------------
package org.eclipse.epf.library.util;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.epf.common.utils.StrUtil;
import org.eclipse.epf.library.LibraryPlugin;
import org.eclipse.epf.library.LibraryResources;
import org.eclipse.epf.library.edit.command.DeleteMethodElementCommand;
import org.eclipse.epf.library.edit.util.TngUtil;
import org.eclipse.epf.library.services.LibraryProcessor;
import org.eclipse.epf.persistence.MethodLibraryPersister;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.dialogs.ListDialog;
import com.ibm.uma.ContentDescription;
import com.ibm.uma.ContentPackage;
import com.ibm.uma.Guidance;
import com.ibm.uma.UmaFactory;
import com.ibm.uma.UmaPackage;
import com.ibm.uma.util.ContentDescriptionFactory;
import com.ibm.uma.util.IMethodLibraryPersister;
public class ConvertGuidanceType {
private static int[] compatibleGuidances = { UmaPackage.CONCEPT,
// UmaPackage.CHECKLIST,
// UmaPackage.EXAMPLE,
UmaPackage.GUIDELINE,
// UmaPackage.ESTIMATE,
// UmaPackage.ESTIMATING_METRIC,
// UmaPackage.ESTIMATION_CONSIDERATIONS,
// UmaPackage.REPORT,
// UmaPackage.TEMPLATE,
UmaPackage.SUPPORTING_MATERIAL,
// UmaPackage.TOOL_MENTOR,
// UmaPackage.WHITEPAPER,
// UmaPackage.TERM_DEFINITION,
// UmaPackage.PRACTICE,
// UmaPackage.REUSABLE_ASSET
};
private static List compatibleGuidancesList = new ArrayList();
static {
for (int i = 0; i < compatibleGuidances.length; i++)
compatibleGuidancesList.add(new Integer(compatibleGuidances[i]));
}
public static Guidance convertGuidance(Guidance oldGuidance, Shell shell,
DeleteMethodElementCommand command) {
if (shell == null)
shell = Display.getCurrent().getActiveShell();
ListDialog dlg = new ListDialog(shell);
dlg.setHeightInChars(5);
dlg.setContentProvider(new ArrayContentProvider());
dlg.setLabelProvider(new LabelProvider() {
public String getText(Object element) {
switch (((Integer) element).intValue()) {
// TODO: refactor these strings (and this whole dialog) into
// library.ui
case UmaPackage.CONCEPT:
return LibraryResources.getString("Library.concept.text"); //$NON-NLS-1$
case UmaPackage.CHECKLIST:
return LibraryResources.getString("Library.checklist.text"); //$NON-NLS-1$
case UmaPackage.EXAMPLE:
return LibraryResources.getString("Library.example.text"); //$NON-NLS-1$
case UmaPackage.GUIDELINE:
return LibraryResources.getString("Library.guideline.text"); //$NON-NLS-1$
case UmaPackage.ESTIMATE:
return LibraryResources.getString("Library.estimate.text"); //$NON-NLS-1$
case UmaPackage.ESTIMATING_METRIC:
return LibraryResources
.getString("Library.estimatingMetric.text"); //$NON-NLS-1$
case UmaPackage.ESTIMATION_CONSIDERATIONS:
return LibraryResources
.getString("Library.estimationConsiderations.text"); //$NON-NLS-1$
case UmaPackage.REPORT:
return LibraryResources.getString("Library.report.text"); //$NON-NLS-1$
case UmaPackage.TEMPLATE:
return LibraryResources.getString("Library.template.text"); //$NON-NLS-1$
case UmaPackage.SUPPORTING_MATERIAL:
return LibraryResources
.getString("Library.supportingMaterial.text"); //$NON-NLS-1$
case UmaPackage.TOOL_MENTOR:
return LibraryResources
.getString("Library.toolMentor.text"); //$NON-NLS-1$
case UmaPackage.WHITEPAPER:
return LibraryResources
.getString("Library.whitepaper.text"); //$NON-NLS-1$
case UmaPackage.TERM_DEFINITION:
return LibraryResources
.getString("Library.termDefinition.text"); //$NON-NLS-1$
case UmaPackage.PRACTICE:
return LibraryResources.getString("Library.practice.text"); //$NON-NLS-1$
case UmaPackage.REUSABLE_ASSET:
return LibraryResources
.getString("Library.reusableAsset.text"); //$NON-NLS-1$
default:
return LibraryResources
.getString("Library.unknownGuidance.text"); //$NON-NLS-1$
}
}
});
List newGuidanceTypeList = getValidNewGuidanceTypes(oldGuidance);
if (newGuidanceTypeList == null) {
LibraryPlugin
.getDefault()
.getMsgDialog()
.displayError(
LibraryResources
.getString("Library.convertGuidanceError.title"), //$NON-NLS-1$
LibraryResources
.getString("Library.unsupportedGuidanceTypeError.msg"), //$NON-NLS-1$
LibraryResources
.formatString(
"Library.unsupportedGuidanceTypeError.reason", StrUtil.toLower(TngUtil.getTypeText(oldGuidance)))); //$NON-NLS-1$
return null;
}
dlg.setInput(newGuidanceTypeList);
dlg.setTitle(LibraryResources
.getString("Library.convertGuidanceDialog.title")); //$NON-NLS-1$
dlg.setMessage(LibraryResources
.getString("Library.convertGuidanceDialog.text")); //$NON-NLS-1$
if (dlg.open() == Dialog.CANCEL)
return null;
Object[] selectionResult = dlg.getResult();
if (selectionResult == null)
return null;
int chosenGuidance = ((Integer) selectionResult[0]).intValue();
Guidance newGuidance = doConvert(oldGuidance, chosenGuidance, command);
return newGuidance;
}
private static Guidance doConvert(Guidance oldGuidance,
int newGuidanceType, DeleteMethodElementCommand command) {
// removedReferencesMap = new HashMap();
Guidance newGuidance = null;
try {
File oldGuidancePathFile = new File(LibraryProcessor.getInstance()
.getLibraryRootPath(), MethodLibraryPersister
.getElementPath(oldGuidance));
// create new guidance
newGuidance = createNewGuidance(newGuidanceType);
// TODO: store oldGuidance's features here, and don't set them until
// after oldguidance is deleted
// copy all data from oldGuidance to newGuidance
Iterator iter = oldGuidance.eClass().getEAllStructuralFeatures()
.iterator();
while (iter.hasNext()) {
EStructuralFeature feature = (EStructuralFeature) iter.next();
Object o = oldGuidance.eGet(feature);
if (feature.getFeatureID() != UmaPackage.DESCRIBABLE_ELEMENT__PRESENTATION
&& feature.getFeatureID() != UmaPackage.METHOD_ELEMENT__GUID)
newGuidance.eSet(feature, o);
}
// store Guid here
String oldGuidanceGuid = oldGuidance.getGuid();
// store old guidance's eContainer
ContentPackage oldGuidanceContainer = (ContentPackage) oldGuidance
.eContainer();
// set container
oldGuidanceContainer.getContentElements().add(newGuidance);
// create presentation
ContentDescription newContentDesc = ContentDescriptionFactory
.createContentDescription(newGuidance);
newGuidance.setPresentation(newContentDesc);
File newGuidancePathFile = new File(LibraryProcessor.getInstance()
.getLibraryRootPath(), MethodLibraryPersister
.getElementPath(newGuidance));
ContentResourceScanner scanner = new ContentResourceScanner(
oldGuidancePathFile);
scanner.setTargetRootPath(newGuidancePathFile);
// copy all presentation data from oldGuidance to newGuidance
ContentDescription oldContentDesc = oldGuidance.getPresentation();
iter = oldContentDesc.eClass().getEAllAttributes().iterator();
while (iter.hasNext()) {
EAttribute attrib = (EAttribute) iter.next();
Object o = oldContentDesc.eGet(attrib);
if (o instanceof String && ((String) o).trim().length() > 0) {
// process links
scanner.resolveResources(newGuidance, (String) o, ""); //$NON-NLS-1$
// scanner.resolveResources(newGuidance, (String) o,
// MethodLibraryPersister.getElementPath(newGuidance));
// //$NON-NLS-1$
}
newContentDesc.eSet(attrib, o);
}
// delete old guidance here - if fails, roll back by deleting new
// guidance
command.execute();
if (!command.executed || command.failed) {
// delete failed, or user selected cancel on the "delete
// references" dialog
// clean up new guidance
// unset container
oldGuidanceContainer.getContentElements().remove(newGuidance);
newGuidance = null;
newContentDesc = null;
return null;
}
// set new guidance's GUID
newGuidance.setGuid(oldGuidanceGuid);
// TODO: update editor contents (eg, if a Task with a Supporting
// Material copyright has open
// editor, the copyright field is not updated when that SM is
// changed to a Concept)
// TODO: on rollback, library may be dirty because of the
// container/contentdesc changes we've done
// fix this by storing all the old guidance's info, then only create
// new guidance after old one has
// been deleted.
// save new guidance and its presentation
IMethodLibraryPersister.FailSafeMethodLibraryPersister persister = ContentDescriptionFactory
.getMethodLibraryPersister().getFailSafePersister();
try {
if (newGuidance.eResource() != null)
persister.save(newGuidance.eResource());
if (newGuidance.getPresentation().eResource() != null)
persister.save(newGuidance.getPresentation().eResource());
if (oldGuidanceContainer.eResource() != null)
persister.save(oldGuidanceContainer.eResource());
persister.commit();
} catch (Exception e) {
persister.rollback();
command.undo();
LibraryPlugin
.getDefault()
.getMsgDialog()
.displayError(
LibraryResources
.getString("Library.convertGuidanceError.title"), //$NON-NLS-1$
LibraryResources
.formatString(
"Library.saveConvertedGuidanceError.msg", newGuidance.getName()), //$NON-NLS-1$
LibraryResources
.getString("Library.error.reason"), //$NON-NLS-1$
e);
newGuidance = null;
}
} catch (Exception ex) {
command.undo();
LibraryPlugin
.getDefault()
.getMsgDialog()
.displayError(
LibraryResources
.getString("Library.convertGuidanceError.title"), //$NON-NLS-1$
LibraryResources
.formatString(
"Library.convertGuidanceError.msg", newGuidance.getName()), //$NON-NLS-1$
LibraryResources.getString("Library.error.reason"), //$NON-NLS-1$
ex);
newGuidance = null;
}
return newGuidance;
}
private static Guidance createNewGuidance(int newGuidanceType) {
switch (newGuidanceType) {
case UmaPackage.CONCEPT:
return UmaFactory.eINSTANCE.createConcept();
case UmaPackage.CHECKLIST:
return UmaFactory.eINSTANCE.createChecklist();
case UmaPackage.EXAMPLE:
return UmaFactory.eINSTANCE.createExample();
case UmaPackage.GUIDELINE:
return UmaFactory.eINSTANCE.createGuideline();
case UmaPackage.ESTIMATE:
return UmaFactory.eINSTANCE.createEstimate();
case UmaPackage.ESTIMATING_METRIC:
return UmaFactory.eINSTANCE.createEstimatingMetric();
case UmaPackage.ESTIMATION_CONSIDERATIONS:
return UmaFactory.eINSTANCE.createEstimationConsiderations();
case UmaPackage.REPORT:
return UmaFactory.eINSTANCE.createReport();
case UmaPackage.TEMPLATE:
return UmaFactory.eINSTANCE.createTemplate();
case UmaPackage.SUPPORTING_MATERIAL:
return UmaFactory.eINSTANCE.createSupportingMaterial();
case UmaPackage.TOOL_MENTOR:
return UmaFactory.eINSTANCE.createToolMentor();
case UmaPackage.WHITEPAPER:
return UmaFactory.eINSTANCE.createWhitepaper();
case UmaPackage.TERM_DEFINITION:
return UmaFactory.eINSTANCE.createTermDefinition();
case UmaPackage.PRACTICE:
return UmaFactory.eINSTANCE.createPractice();
case UmaPackage.REUSABLE_ASSET:
return UmaFactory.eINSTANCE.createReusableAsset();
default:
return null;
}
}
public static List getValidNewGuidanceTypes(Guidance oldGuidance) {
if (oldGuidance == null)
return null;
Integer oldGuidanceClassID = new Integer(oldGuidance.eClass()
.getClassifierID());
if (!compatibleGuidancesList.contains(oldGuidanceClassID))
return null;
List guidanceList = new ArrayList();
for (int i = 0; i < compatibleGuidances.length; i++) {
Integer compatibleGuidanceTypeClassID = new Integer(
compatibleGuidances[i]);
if (!oldGuidanceClassID.equals(compatibleGuidanceTypeClassID))
guidanceList.add(compatibleGuidanceTypeClassID);
}
return guidanceList;
}
}