Skip to main content
summaryrefslogtreecommitdiffstats
blob: a6dc2b2cf028529def49b15fcdace3556d33791e (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
/*******************************************************************************
 * Copyright (c) 2003, 2004 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
 *******************************************************************************/
/*
 * Created on Dec 3, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.eclipse.jst.j2ee.internal.common.operations;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jst.j2ee.application.Application;
import org.eclipse.jst.j2ee.common.XMLResource;
import org.eclipse.jst.j2ee.ejb.AssemblyDescriptor;
import org.eclipse.jst.j2ee.ejb.EJBJar;
import org.eclipse.jst.j2ee.internal.common.J2EECommonMessages;
import org.eclipse.jst.j2ee.webapplication.WebApp;
import org.eclipse.wst.common.frameworks.internal.operations.WTPOperation;
import org.eclispe.wst.common.frameworks.internal.plugin.WTPCommonPlugin;


/**
 * @author DABERG
 * 
 * To change the template for this generated type comment go to Window>Preferences>Java>Code
 * Generation>Code and Comments
 */
public class AddSecurityRoleOperationDataModel extends J2EEModelModifierOperationDataModel {
	/**
	 * Required - The name of the new role.
	 * 
	 * @see String
	 */
	public static final String ROLE_NAME = "AddSecurityRoleOperationDataModel.ROLE_NAME"; //$NON-NLS-1$

	/**
	 * Optional - The description of the new role.
	 * 
	 * @see String
	 */
	public static final String ROLE_DESCRIPTION = "AddSecurityRoleOperationDataModel.ROLE_DESCRIPTION"; //$NON-NLS-1$

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.common.frameworks.internal.operation.WTPOperationDataModel#getDefaultOperation()
	 */
	public WTPOperation getDefaultOperation() {
		return new AddSecurityRoleOperation(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.common.internal.emfworkbench.operation.ModelModifierOperationDataModel#initValidBaseProperties()
	 */
	protected void initValidBaseProperties() {
		super.initValidBaseProperties();
		addValidBaseProperty(ROLE_NAME);
		addValidBaseProperty(ROLE_DESCRIPTION);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.common.frameworks.internal.operation.WTPOperationDataModel#doValidateProperty(java.lang.String)
	 */
	protected IStatus doValidateProperty(String propertyName) {
		if (propertyName.equals(ROLE_NAME))
			return validateRoleName(getStringProperty(propertyName));
		return super.doValidateProperty(propertyName);
	}

	/**
	 * @param roleName
	 * @return
	 */
	private IStatus validateRoleName(String roleName) {
		if (roleName.length() == 0) {
			String msg = J2EECommonMessages.getResourceString(J2EECommonMessages.ERR_SECURITY_ROLE_EMPTY);
			return WTPCommonPlugin.createErrorStatus(msg);
		}
		boolean exists = roleExists(roleName, getDeploymentDescriptorRoot());
		if (exists) {
			String msg = J2EECommonMessages.getResourceString(J2EECommonMessages.ERR_SECURITY_ROLE_EXIST, new String[]{roleName});
			return WTPCommonPlugin.createErrorStatus(msg);
		}
		return WTPCommonPlugin.OK_STATUS;
	}

	/**
	 * @param roleName
	 * @param root
	 * @return
	 */
	private boolean roleExists(String roleName, EObject root) {
		switch (getDeploymentDescriptorType()) {
			case XMLResource.APPLICATION_TYPE :
				return roleExists(roleName, (Application) root);
			case XMLResource.EJB_TYPE :
				return roleExists(roleName, (EJBJar) root);
			case XMLResource.WEB_APP_TYPE :
				return roleExists(roleName, (WebApp) root);
		}
		return false;
	}

	/**
	 * @param roleName
	 * @param jar
	 * @return
	 */
	private boolean roleExists(String roleName, EJBJar jar) {
		AssemblyDescriptor descriptor = jar.getAssemblyDescriptor();
		return descriptor != null && descriptor.getSecurityRoleNamed(roleName) != null;
	}

	/**
	 * @param roleName
	 * @param application
	 * @return
	 */
	private boolean roleExists(String roleName, Application application) {
		return application.getSecurityRoleNamed(roleName) != null;
	}

	/**
	 * @param roleName
	 * @param webApp
	 * @return
	 */
	private boolean roleExists(String roleName, WebApp webApp) {
		return webApp.getSecurityRoleNamed(roleName) != null;
	}
}

Back to the top