Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1910b1b9aee6a4e13d71a1d413ecf880745464a3 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*******************************************************************************
 * Copyright (c) 2007, 2008 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 API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20071105   196997 pmoogk@ca.ibm.com - Peter Moogk
 * 20071218   209858 epeters@ca.ibm.com - Eric Peters, Enhancing service policy framework and UI
 * 20080318   223118 ericdp@ca.ibm.com - Eric D. Peters, metadata required in WS-I profiles to support editors & validators
 * 20080318   221578 pmoogk@ca.ibm.com - Peter Moogk
 *******************************************************************************/
package org.eclipse.wst.ws.internal.preferences;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.wst.ws.internal.plugin.WSPlugin;
import org.eclipse.wst.ws.service.policy.IPolicyState;
import org.eclipse.wst.ws.service.policy.IServicePolicy;
import org.eclipse.wst.ws.service.policy.ServicePolicyActivator;
import org.eclipse.wst.ws.service.policy.ServicePolicyPlatform;
import org.eclipse.wst.ws.service.policy.listeners.IPolicyPlatformLoadListener;

public class MigrateWSIpreferencesLoadListener implements IPolicyPlatformLoadListener
{
  private static final String[] ENUM_ID_VALUES = { "org.eclipse.wst.sug.require", "org.eclipse.wst.sug.suggest", "org.eclipse.wst.sug.ignore"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
  private static final String[] CONTEXT_IDS    = { PersistentWSIContext.STOP_NON_WSI, PersistentWSIContext.WARN_NON_WSI, PersistentWSIContext.IGNORE_NON_WSI };
  
  public void load()
  {
    // We need to check to see if the user has set any WSI preferences.
    // If not we want to default WSI preferences to any old WSI preferences that
    // have been set.
    
    IEclipsePreferences   preferences  = new InstanceScope().getNode( ServicePolicyActivator.PLUGIN_ID );    
    String                apValue      = preferences.get( WSIServicePoliciesConstants.ServicePolicyID_AP10 + "." + IPolicyState.DefaultValueKey, null ); //$NON-NLS-1$
    String                ssbpValue    = preferences.get( WSIServicePoliciesConstants.ServicePolicyID_SSBP10 + "." + IPolicyState.DefaultValueKey, null ); //$NON-NLS-1$
    Preferences           oldPrefs     = WSPlugin.getInstance().getPluginPreferences();
    String[]              oldPropNames = oldPrefs.propertyNames();
    
    if( apValue == null )
    {
      ServicePolicyPlatform platform     = ServicePolicyPlatform.getInstance();
      IServicePolicy        apPolicy     = platform.getServicePolicy( WSIServicePoliciesConstants.ServicePolicyID_AP10 );
      IPolicyState          apState      = apPolicy.getPolicyState();
      
      if( indexOfString( "nonWSIAPCompliance", oldPropNames ) != -1 ) //$NON-NLS-1$
      {
        // We have an old AP value so we will use it's value as the default for settings.
        String oldApValue = oldPrefs.getString( "nonWSIAPCompliance" ); //$NON-NLS-1$
        int    oldApIndex = indexOfString( oldApValue, CONTEXT_IDS );
        String newApValue = null;
        
        if( oldApIndex < ENUM_ID_VALUES.length )
        {
          newApValue = ENUM_ID_VALUES[oldApIndex];
        }
        
        if( newApValue != null )
        {
          apState.putValue( IPolicyState.DefaultValueKey, newApValue );
        }
      }
      		// also need to check if any project specific preferences were set
			QualifiedName name = new QualifiedName(WSPlugin.ID,
					"nonWSIAPCompliance"); //$NON-NLS-1$
			IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
					.getProjects();
			for (int i = 0; i < projects.length; i++) {
				try {
					IProject project = projects[i];
					String oldProperty = null;
						oldProperty = project.getPersistentProperty(name);
					if (oldProperty != null && !oldProperty.equals("")) { //$NON-NLS-1$
						int oldApIndex = indexOfString(oldProperty, CONTEXT_IDS);
						String newApValue = null;
						if (oldApIndex != -1 && oldApIndex < ENUM_ID_VALUES.length) {
							newApValue = ENUM_ID_VALUES[oldApIndex];
						}

						if (newApValue != null) {
							apState = apPolicy.getPolicyState(project);
							platform.setProjectPreferencesEnabled(project, true);
					        apState.putValue( IPolicyState.DefaultValueKey, newApValue );
						}
					}
				} catch (CoreException e) {
					//ignore this project
				}
			}
    }


    
    if( ssbpValue == null )
    {
      ServicePolicyPlatform platform     = ServicePolicyPlatform.getInstance();
      IServicePolicy        ssbpPolicy   = platform.getServicePolicy( WSIServicePoliciesConstants.ServicePolicyID_SSBP10 );
      IPolicyState          ssbpState    = ssbpPolicy.getPolicyState();
      
      if( indexOfString( "nonWSISSBPCompliance", oldPropNames ) != -1 ) //$NON-NLS-1$
      {
        // We have an old SSBP value so we will use it's value as the default for settings.
        String oldSSBPValue = oldPrefs.getString( "nonWSISSBPCompliance" ); //$NON-NLS-1$
        int    oldSSBPIndex = indexOfString( oldSSBPValue, CONTEXT_IDS );
        String newSSBPValue = null;
        
        if( oldSSBPIndex < ENUM_ID_VALUES.length )
        {
          newSSBPValue = ENUM_ID_VALUES[oldSSBPIndex];
        }
        
        if( newSSBPValue != null )
        {
        	ssbpState.putValue( IPolicyState.DefaultValueKey, newSSBPValue );
        }
      }
		// also need to check if any project specific preferences were set
		QualifiedName name = new QualifiedName(WSPlugin.ID,
				"nonWSISSBPCompliance"); //$NON-NLS-1$
		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
				.getProjects();
		for (int i = 0; i < projects.length; i++) {
			try {
				IProject project = projects[i];
				String oldProperty = null;
					oldProperty = project.getPersistentProperty(name);
				if (oldProperty != null && !oldProperty.equals("")) { //$NON-NLS-1$
					int oldSSBPIndex = indexOfString(oldProperty, CONTEXT_IDS);
					String newSSBPValue = null;
					if (oldSSBPIndex != -1 && oldSSBPIndex < ENUM_ID_VALUES.length) {
						newSSBPValue = ENUM_ID_VALUES[oldSSBPIndex];
					}

					if (newSSBPValue != null) {
						ssbpState = ssbpPolicy.getPolicyState(project);
						platform.setProjectPreferencesEnabled(project, true);
						ssbpState.putValue( IPolicyState.DefaultValueKey, newSSBPValue );
					}
				}
			} catch (CoreException e) {
				//ignore this project
			}
		}

    }
  }
  
  private int indexOfString( String target, String[] values )
  {
    int result = -1;
    
    for( int index = 0; index< values.length; index++ )
    {
      String value = values[index];
      
      if( value.equals( target ) )
      {
        result = index;
        break;
      }
    }
    
    return result;
  }
}

Back to the top