Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7ea633989a5fa0c186a2ed95cc4f185344172414 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*******************************************************************************
 * Copyright (c) 2001, 2007 Oracle 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:
 *     Oracle Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.core.internal.jsflibraryregistry.util;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.xmi.XMLResource;
import org.eclipse.jst.jsf.core.internal.JSFCorePlugin;
import org.eclipse.jst.jsf.core.internal.Messages;
import org.eclipse.jst.jsf.core.internal.jsflibraryconfig.JSFLibraryRegistryUtil;

class MigrateV1toV2Operation extends VersionUpgradeOperation {

	private final URI		_v1Registry;
	private final URI		_v2Registry;
	
	/**
	 * @param label
	 * @param v1Registry
	 * @param v2Registry 
	 */
	public MigrateV1toV2Operation(String label, URI v1Registry, URI v2Registry) {
		super(label, 1, 2);
		_v1Registry = v1Registry;
		_v2Registry = v2Registry;
	}

	public IStatus doCommit() {
		JSFLibraryRegistryUpgradeUtil.deleteFile(_v1Registry.toFileString());
		return Status.OK_STATUS;
	}

	public IStatus doExecute(IProgressMonitor monitor, IAdaptable info)
	{
		JSFLibraryRegistryUpgradeUtil.copyFile
			(_v1Registry.toFileString(), JSFLibraryRegistryUpgradeUtil.getBackupFileName(_v1Registry.toFileString()));
		JSFLibraryRegistryResourceFactoryImpl resourceFactory = new JSFLibraryRegistryResourceFactoryImpl();
		JSFLibraryRegistryResourceImpl res = (JSFLibraryRegistryResourceImpl)resourceFactory.createResource(_v1Registry);
		try {
			URI newRegURI = 
				JSFLibraryRegistryUpgradeUtil.getRegistryURI
					(JSFLibraryRegistryUpgradeUtil.JSF_LIBRARY_REGISTRY_V2_URL);
			Map options = new HashMap();
			//disable notifications during load to avoid changing stored default implementation
			options.put(XMLResource.OPTION_DISABLE_NOTIFY, Boolean.TRUE);
			res.load(options);
			//if we got this far then the registry was empty
			//"upgrade" to v2 and then delete old.   no point in upgrade status being sent
			JSFLibraryRegistryUtil.getInstance().saveJSFLibraryRegistry();
			JSFLibraryRegistryUpgradeUtil.copyFile(_v1Registry.toFileString(), newRegURI.toFileString());//save as v2 file	
			JSFLibraryRegistryUpgradeUtil.deleteFile(_v1Registry.toFileString());

			return new UpgradeStatus();//all is ok and no need to alert user
			
		} catch(IOException ioe) {
			//this was expected... if there was actual v1 contents in the regsistry... upgrade by saving
			//perform save which will lose the ID
			try {
				res.save(Collections.EMPTY_MAP);
				//create v2 xml file
				URI newRegURI = 
					JSFLibraryRegistryUpgradeUtil.getRegistryURI
						(JSFLibraryRegistryUpgradeUtil.JSF_LIBRARY_REGISTRY_V2_URL);
				JSFLibraryRegistryUpgradeUtil.copyFile(_v1Registry.toFileString(), newRegURI.toFileString());
				//delete upgraded v1
				JSFLibraryRegistryUpgradeUtil.deleteFile(_v1Registry.toFileString());
				//restore backup to v1 name
				JSFLibraryRegistryUpgradeUtil.copyFile(_v1Registry.toFileString().concat(".bkp"), _v1Registry.toFileString());
				//Alert end user
				return new UpgradeStatus(IStatus.OK, true, Messages.JSFRegistryMigration05_to_10_customMessage);
			} catch(IOException e) {
				JSFCorePlugin.log(IStatus.ERROR, "Error during repository upgrade from v1 to v2", e);
				return new UpgradeStatus(IStatus.ERROR, false, 	
						Messages.JSFRegistryMigrationCannot05_to_10_customMessage);
			}
		}
		//return ;
	}

	public IStatus doRedo(IProgressMonitor monitor, IAdaptable info)
			throws ExecutionException {
		return doExecute(monitor, info);
	}

	
	public boolean canUndo() {
		// commit is undoable for this operation
		return super.canUndo() && !hasCommitted();
	}

	public IStatus doUndo(IProgressMonitor monitor, IAdaptable info)
			throws ExecutionException 
	{
		//restore backup to v1 name
		JSFLibraryRegistryUpgradeUtil.copyFile(_v1Registry.toFileString().concat(".bkp"), _v1Registry.toFileString());

		// delete the new registry 
		JSFLibraryRegistryUpgradeUtil.deleteFile(_v2Registry.toFileString());
		
		//and the backup
		JSFLibraryRegistryUpgradeUtil.deleteFile(_v1Registry.toFileString().concat(".bkp"));
		
		return Status.OK_STATUS;
	}
}

Back to the top