001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Admin/Sources/es/udc/mypersonalizer/admin/model/actions/database/InitDatabaseAction.java,v 1.1.1.1 2004/03/25 12:08:39 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:39 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.admin.model.actions.database;
042:
043: import java.io.Serializable;
044: import java.util.Collection;
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.HashMap;
048: import java.util.Map;
049: import java.util.Arrays;
050: import java.util.Calendar;
051:
052: import es.udc.mypersonalizer.kernel.conventions.UserAndGroupConventions;
053: import es.udc.mypersonalizer.kernel.conventions.ServiceConventions;
054: import es.udc.mypersonalizer.kernel.model.properties.Property;
055: import es.udc.mypersonalizer.kernel.model.properties.PropertyStructure;
056: import es.udc.mypersonalizer.kernel.model.repository.interfaces.RepositoryAccessorFactory;
057: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserRegistrationInformation;
058: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServicePrototype;
059: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserGroup;
060: import es.udc.mypersonalizer.kernel.model.repository.interfaces.WorkspaceDefinition;
061: import es.udc.mypersonalizer.kernel.model.repository.interfaces.WorkspaceLayout;
062: import es.udc.mypersonalizer.kernel.model.repository.interfaces.DesktopLayout;
063: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserRegistrationInformationAccessor;
064: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServicePrototypeAccessor;
065: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserGroupAccessor;
066: import es.udc.mypersonalizer.kernel.model.repository.interfaces.WorkspaceDefinitionAccessor;
067: import es.udc.mypersonalizer.kernel.model.repository.interfaces.WorkspaceLayoutAccessor;
068: import es.udc.mypersonalizer.kernel.model.repository.interfaces.DesktopLayoutAccessor;
069: import es.udc.mypersonalizer.kernel.model.editors.EditorFactory;
070: import es.udc.mypersonalizer.kernel.model.editors.WorkspaceLayoutEditor;
071: import es.udc.mypersonalizer.kernel.model.editors.DesktopLayoutEditor;
072: import es.udc.mypersonalizer.kernel.model.editors.UserRegistrationInformationEditor;
073: import es.udc.mypersonalizer.kernel.model.actions.ActionProcessorSingleton;
074: import es.udc.mypersonalizer.kernel.model.actions.AbstractAction;
075: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
076:
077: import es.udc.mypersonalizer.portal.model.types.WizardEvent;
078: import es.udc.mypersonalizer.portal.model.encryption.PasswordManagerSingleton;
079: import es.udc.mypersonalizer.portal.model.permissions.UserCredentials;
080:
081: /**
082: * Model action for initialize the database with special objects. This action
083: * creates the mypersonalizer and anonymous special users, a default
084: * <code>DesktopLayout</code> with one default <ode>WorkspaceLayout</code>
085: * and the <code>everybody</code> user group.
086: *
087: * @author Abel Iago Toral Quiroga
088: * @author Daniel Fernandez
089: * @since 1.0
090: */
091: public class InitDatabaseAction extends AbstractAction {
092:
093: /**
094: * A constant for the default workspace definition type
095: */
096: private static final String INITIAL_WORKSPACE_TYPE = "PUBLIC";
097:
098: /**
099: * A constant for the default workspace definition name, which
100: * will initially be set to the same value as the "workspace type".
101: */
102: private static final String INITIAL_WORKSPACE_NAME = "PUBLIC";
103:
104: /**
105: * Initializes the database.
106: * @param event is not used.
107: * @throws <code>InternalErrorException</code> if a severe error ocurred
108: * @return always null.
109: */
110: public Serializable execute(Serializable event)
111: throws InternalErrorException {
112:
113: try {
114:
115: /* ***** Step 0 : Initialize variables and resources ***** */
116:
117: /* Create needed accessors */
118: UserRegistrationInformationAccessor userRegistrationInformationAccessor = RepositoryAccessorFactory
119: .getInstance()
120: .createUserRegistrationInformationAccessor();
121:
122: ServicePrototypeAccessor serviceProtoypeAccessor = RepositoryAccessorFactory
123: .getInstance().createServicePrototypeAccessor();
124:
125: UserGroupAccessor userGroupAccessor = RepositoryAccessorFactory
126: .getInstance().createUserGroupAccessor();
127:
128: WorkspaceDefinitionAccessor workspaceDefinitionAccessor = RepositoryAccessorFactory
129: .getInstance().createWorkspaceDefinitionAccessor();
130:
131: WorkspaceLayoutAccessor workspaceLayoutAccessor = RepositoryAccessorFactory
132: .getInstance().createWorkspaceLayoutAccessor();
133:
134: DesktopLayoutAccessor desktopLayoutAccessor = RepositoryAccessorFactory
135: .getInstance().createDesktopLayoutAccessor();
136:
137: /* Create a Collection with all service identifiers */
138: Iterator servicePrototypesIterator = serviceProtoypeAccessor
139: .findAllServicePrototypes().iterator();
140:
141: Collection allServiceIdentifiers = new ArrayList();
142:
143: while (servicePrototypesIterator.hasNext()) {
144: ServicePrototype servicePrototype = (ServicePrototype) servicePrototypesIterator
145: .next();
146: allServiceIdentifiers.add(servicePrototype
147: .getIdentifier());
148: }
149:
150: /* Create needed editors */
151: EditorFactory editorFactory = EditorFactory.getInstance();
152:
153: WorkspaceLayoutEditor workspaceLayoutEditor = editorFactory
154: .createWorkspaceLayoutEditor(null);
155:
156: DesktopLayoutEditor desktopLayoutEditor = editorFactory
157: .createDesktopLayoutEditor(null);
158:
159: UserRegistrationInformationEditor userRegistrationInformationEditor = editorFactory
160: .createUserRegistrationInformationEditor(null);
161:
162: /* ***** Step 1 : Retrieve the everybody user group ***** */
163:
164: UserGroup everybodyUserGroup = userGroupAccessor
165: .findGroupByName(UserAndGroupConventions.EVERYBODY_GROUP_NAME);
166:
167: /* ***** Step 2 : Create a "public" workspace definition ***** */
168:
169: WorkspaceDefinition initialWorkspaceDefinition = new WorkspaceDefinition(
170: INITIAL_WORKSPACE_TYPE);
171:
172: Collection defaultPermissions = Arrays
173: .asList(new Long[] { everybodyUserGroup
174: .getUserGroupIdentifier() });
175:
176: initialWorkspaceDefinition
177: .setAvailableServiceIdentifiers(allServiceIdentifiers);
178: initialWorkspaceDefinition
179: .setAllowedToAddUserGroupIdentifiers(defaultPermissions);
180: initialWorkspaceDefinition
181: .setAllowedToRemoveUserGroupIdentifiers(defaultPermissions);
182:
183: workspaceDefinitionAccessor
184: .addWorkspaceDefinition(initialWorkspaceDefinition);
185:
186: /* ***** Step 3 : Create an empty workspace layout ***** */
187:
188: Property initialWorkspaceLayoutProperty = workspaceLayoutEditor
189: .createEmptyProperty();
190:
191: workspaceLayoutEditor
192: .setProperty(initialWorkspaceLayoutProperty);
193:
194: workspaceLayoutEditor
195: .setWorkspaceType(INITIAL_WORKSPACE_TYPE);
196:
197: workspaceLayoutEditor
198: .setWorkspaceName(INITIAL_WORKSPACE_NAME);
199:
200: workspaceLayoutEditor.setServiceKeys(new HashMap());
201:
202: workspaceLayoutEditor.setLayoutType("default");
203:
204: workspaceLayoutEditor.updateTimeStamp();
205:
206: WorkspaceLayout initialWorkspaceLayout = workspaceLayoutAccessor
207: .addWorkspaceLayout(initialWorkspaceLayoutProperty);
208:
209: /* **** Step 4 : Create a desktop layout with previous workspace */
210:
211: Property initialDesktopLayoutProperty = desktopLayoutEditor
212: .createEmptyProperty();
213: desktopLayoutEditor
214: .setProperty(initialDesktopLayoutProperty);
215:
216: desktopLayoutEditor.setWorkspaceLayoutIdentifiers(Arrays
217: .asList(new Long[] { initialWorkspaceLayout
218: .getIdentifier() }));
219:
220: desktopLayoutEditor
221: .setDefaultWorkspaceLayoutIdentifier(initialWorkspaceLayout
222: .getIdentifier());
223:
224: desktopLayoutEditor.updateTimeStamp();
225:
226: DesktopLayout initialDesktopLayout = desktopLayoutAccessor
227: .addDesktopLayout(initialDesktopLayoutProperty);
228:
229: /* ***** Step 5 : Create users mypersonalizer and anonymous *** */
230:
231: /* Create mypersonalizer user */
232: String mypersonalizerEncryptedPassword = PasswordManagerSingleton
233: .getInstance()
234: .getPasswordToStore(
235: UserAndGroupConventions.MYPERSONALIZER_LOGIN_NAME);
236:
237: Property mypersonalizerProperty = userRegistrationInformationEditor
238: .createEmptyProperty();
239: userRegistrationInformationEditor
240: .setProperty(mypersonalizerProperty);
241:
242: userRegistrationInformationEditor
243: .setLoginName(UserAndGroupConventions.MYPERSONALIZER_LOGIN_NAME);
244:
245: userRegistrationInformationEditor
246: .setStoredPassword(mypersonalizerEncryptedPassword);
247:
248: userRegistrationInformationEditor
249: .setLastSignInDate(Calendar.getInstance());
250:
251: userRegistrationInformationEditor
252: .setRegistrationDate(Calendar.getInstance());
253:
254: userRegistrationInformationEditor
255: .setDesktopLayoutPropertyIdentifier(initialDesktopLayout
256: .getIdentifier());
257:
258: UserRegistrationInformation mypersonalizerUserRegistrationInformation = new UserRegistrationInformation(
259: UserAndGroupConventions.MYPERSONALIZER_LOGIN_NAME,
260: mypersonalizerProperty);
261:
262: userRegistrationInformationAccessor
263: .addUserRegistrationInformation(mypersonalizerUserRegistrationInformation);
264:
265: /* Create anonymous user */
266: String anonymousEncryptedPassword = UserAndGroupConventions.ANONYMOUS_LOGIN_NAME;
267:
268: UserCredentials anonymousUserCredentials = new UserCredentials(
269: UserAndGroupConventions.ANONYMOUS_LOGIN_NAME,
270: defaultPermissions);
271:
272: Property anonymousProperty = userRegistrationInformationEditor
273: .createEmptyProperty();
274: userRegistrationInformationEditor
275: .setProperty(anonymousProperty);
276:
277: userRegistrationInformationEditor
278: .setLoginName(UserAndGroupConventions.ANONYMOUS_LOGIN_NAME);
279:
280: userRegistrationInformationEditor
281: .setStoredPassword(anonymousEncryptedPassword);
282:
283: Map wizardEventProperties = new HashMap();
284: Iterator propertiesIterator = ((PropertyStructure[]) anonymousProperty
285: .getValuesAsObject())[0].getAsMap().entrySet()
286: .iterator();
287:
288: while (propertiesIterator.hasNext()) {
289:
290: Map.Entry entry = (Map.Entry) propertiesIterator.next();
291: String currentPropertyName = (String) entry.getKey();
292: Property currentProperty = (Property) entry.getValue();
293: String[] currentPropertyValues = currentProperty
294: .getValuesAsString();
295: wizardEventProperties.put(currentPropertyName,
296: currentPropertyValues);
297: }
298:
299: WizardEvent signUpEvent = new WizardEvent(
300: anonymousUserCredentials,
301: wizardEventProperties,
302: ServiceConventions.USER_REGISTRATION_INFORMATION_SERVICE_IDENTIFIER,
303: "updateSimpleProperties", "0", null);
304:
305: ActionProcessorSingleton.getInstance().execute(
306: "SignUpAction", signUpEvent);
307:
308: return null;
309:
310: } catch (InternalErrorException e) {
311: throw e;
312: } catch (Exception e) {
313: throw new InternalErrorException(e);
314: }
315: }
316: }
|