Create language configuration option for user

FEA: ItEr75S07UserSettings
This commit is contained in:
Cristina Alvarino 2011-06-28 16:09:47 +02:00 committed by Manuel Rego Casasnovas
parent 2ee45a57a9
commit 6dcbf26ef4
9 changed files with 406 additions and 2 deletions

View file

@ -0,0 +1,47 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2011 ComtecSF, S.L.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.business.users.entities;
import static org.navalplanner.business.i18n.I18nHelper._;
/**
* Available languages.
*
* @author Cristina Alavarino Perez <cristina.alvarino@comtecsf.es>
* @author Ignacio Diaz Teijido <ignacio.diaz@comtecsf.es>
*/
public enum Language {
BROWSER_LANGUAGE(_("Use browser language configuration ")),
GALICIAN_LANGUAGE(_("Galician")),
SPANISH_LANGUAGE(_("Spanish")),
ENGLISH_LANGUAGE(_("English"));
private final String displayName;
private Language(String displayName) {
this.displayName = displayName;
}
public String getDisplayName() {
return displayName;
}
}

View file

@ -44,6 +44,8 @@ public class User extends BaseEntity {
private String password="";
private Language applicationLanguage = Language.BROWSER_LANGUAGE;
private Set<UserRole> roles = new HashSet<UserRole>();
private Set<Profile> profiles = new HashSet<Profile>();
@ -197,4 +199,12 @@ public class User extends BaseEntity {
return lastConnectedScenario;
}
public Language getApplicationLanguage() {
return applicationLanguage;
}
public void setApplicationLanguage(Language applicationLanguage) {
this.applicationLanguage = applicationLanguage;
}
}

View file

@ -18,4 +18,11 @@
columnDataType="BOOLEAN" />
</changeSet>
<changeSet id="add-application-language" author="calvarinop">
<comment>Add new column to store the language of application for this user</comment>
<addColumn tableName="user_table">
<column name="application_language" type="INTEGER" defaultValue="0"/>
</addColumn>
</changeSet>
</databaseChangeLog>

View file

@ -19,11 +19,15 @@
</generator>
</id>
<version name="version" access="property" type="long" />
<property name="loginName" not-null="true" unique="true"
column="login_name" />
<property name="loginName" not-null="true" unique="true" column="login_name" />
<property name="password" not-null="true"/>
<property name="email"/>
<property name="disabled"/>
<property name="applicationLanguage" column="application_language">
<type name="org.hibernate.type.EnumType">
<param name="enumClass">org.navalplanner.business.users.entities.Language</param>
</type>
</property>
<!-- Index created in a database-object section -->
<set name="roles" table="roles_table">

View file

@ -338,6 +338,9 @@ public class CustomMenuController extends Div implements IMenuItemsRegister {
subItem(_("Project Costs Per Resource"),"/reports/orderCostsPerResource.zul", "15-informes.html"),
subItem(_("Task Scheduling Status In Project"),"/reports/workingArrangementsPerOrderReport.zul","15-informes.html"),
subItem(_("Materials Needs At Date"),"/reports/timeLineMaterialReport.zul","15-informes.html"));
topItem(_("My account"), "", "",
subItem(_("Settings"), "/users/settings.zul", ""));
}
private Vbox getRegisteredItemsInsertionPoint() {

View file

@ -0,0 +1,45 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2011 ComtecSF, S.L.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.web.users;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.users.entities.Language;
import org.navalplanner.business.users.entities.User;
/**
* Model for UI operations related to user settings
*
* @author Cristina Alvarino Perez <cristina.alvarino@comtecsf.es>
* @author Ignacio Diaz Teijido <ignacio.diaz@comtecsf.es>
*/
public interface ISettingsModel {
void setApplicationLanguage(Language applicationLanguage);
Language getApplicationLanguage();
User findByLoginUser(String login);
void initEdit(User user);
void confirmSave() throws ValidationException;
User getUser();
}

View file

@ -0,0 +1,112 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2011 ComtecSF, S.L.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.web.users;
import static org.navalplanner.web.I18nHelper._;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.users.entities.Language;
import org.navalplanner.business.users.entities.User;
import org.navalplanner.web.common.ConfigurationController;
import org.navalplanner.web.common.IMessagesForUser;
import org.navalplanner.web.common.Level;
import org.navalplanner.web.common.MessagesForUser;
import org.navalplanner.web.security.SecurityUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
/**
* Controller for user settings
*
* @author Cristina Alvarino Perez <cristina.alvarino@comtecsf.es>
* @author Ignacio Diaz Teijido <ignacio.diaz@comtecsf.es>
*/
public class SettingsController extends GenericForwardComposer {
private static final Log LOG = LogFactory
.getLog(ConfigurationController.class);
private IMessagesForUser messages;
private Component messagesContainer;
private Combobox applicationLanguage;
private ISettingsModel settingsModel;
@Override
public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
comp.setVariable("settingsController", this, true);
messages = new MessagesForUser(messagesContainer);
User user = settingsModel.findByLoginUser(SecurityUtils
.getSessionUserLoginName());
settingsModel.initEdit(user);
appendAllLanguages(applicationLanguage);
applicationLanguage.setSelectedIndex(settingsModel.getUser()
.getApplicationLanguage().ordinal());
}
private void appendAllLanguages(Combobox combo) {
for (Language language : getLanguageNames()) {
Comboitem item = combo.appendItem(_(language.getDisplayName()));
item.setValue(language);
}
}
public Language[] getLanguageNames() {
return Language.values();
}
public boolean save() {
try {
settingsModel.setApplicationLanguage(getSelectedLanguage());
settingsModel.confirmSave();
messages.showMessage(Level.INFO, _("Settings saved"));
applicationLanguage.setSelectedItem(applicationLanguage
.getSelectedItem());
return true;
} catch (ValidationException e) {
messages.showInvalidValues(e);
}
return false;
}
private Language getSelectedLanguage() {
Comboitem selectedItem = applicationLanguage.getSelectedItem();
if (selectedItem != null) {
return (Language) selectedItem.getValue();
}
return null;
}
private User getUser()
{
return settingsModel.getUser();
}
}

View file

@ -0,0 +1,115 @@
/*
* This file is part of NavalPlan
*
* Copyright (C) 2011 ComtecSF, S.L.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.navalplanner.web.users;
import org.apache.commons.lang.Validate;
import org.navalplanner.business.common.exceptions.InstanceNotFoundException;
import org.navalplanner.business.common.exceptions.ValidationException;
import org.navalplanner.business.users.daos.IUserDAO;
import org.navalplanner.business.users.entities.Language;
import org.navalplanner.business.users.entities.Profile;
import org.navalplanner.business.users.entities.User;
import org.navalplanner.business.users.entities.UserRole;
import org.navalplanner.web.common.concurrentdetection.OnConcurrentModification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* Model for UI operations related to user settings
*
* @author Cristina Alvarino Perez <cristina.alvarino@comtecsf.es>
* @author Ignacio Diaz Teijido <ignacio.diaz@comtecsf.es>
*/
@Service
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@OnConcurrentModification(goToPage = "/users/settings.zul")
public class SettingsModel implements ISettingsModel {
@Autowired
private IUserDAO userDAO;
private User user;
@Override
public Language getApplicationLanguage() {
return user.getApplicationLanguage();
}
@Override
public void setApplicationLanguage(Language applicationLanguage) {
this.user.setApplicationLanguage(applicationLanguage);
}
@Override
@Transactional(readOnly = true)
public User findByLoginUser(String login) {
try {
return user = userDAO.findByLoginName(login);
} catch (InstanceNotFoundException e) {
throw new RuntimeException(e);
}
}
@Override
@Transactional(readOnly = true)
public void initEdit(User user) {
Validate.notNull(user);
this.user = getFromDB(user);
}
@Transactional(readOnly = true)
private User getFromDB(User user) {
return getFromDB(user.getId());
}
private User getFromDB(Long id) {
try {
User result = userDAO.find(id);
forceLoadEntities(result);
return result;
} catch (InstanceNotFoundException e) {
throw new RuntimeException(e);
}
}
private void forceLoadEntities(User user) {
user.getLoginName();
for (UserRole each : user.getRoles()) {
each.name();
}
for (Profile each : user.getProfiles()) {
each.getProfileName();
}
}
@Override
@Transactional
public void confirmSave() throws ValidationException {
userDAO.save(user);
}
@Override
public User getUser() {
return this.user;
}
}

View file

@ -0,0 +1,61 @@
<!--
This file is part of NavalPlan
Copyright (C) 2011 ComtecSF, S.L.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with this program. If not, see
<http://www.gnu.org/licenses/>.
-->
<?page title="${i18n:_('NavalPlan: Settings')}"?>
<?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?>
<?taglib uri="http://www.zkoss.org/dsp/web/core" prefix="c"?>
<?page id="List"?>
<?init class="org.zkoss.zk.ui.util.Composition" arg0="/common/layout/template.zul"?>
<?link rel="stylesheet" type="text/css" href="/common/css/navalplan.css"?>
<?link rel="stylesheet" type="text/css" href="/common/css/navalplan_zk.css"?>
<?link rel="stylesheet" type="text/css" href="/resources/css/resources.css"?>
<?variable-resolver class="org.zkoss.zkplus.spring.DelegatingVariableResolver"?>
<zk>
<window id="settingsWindow" self="@{define(content)}"
apply="org.navalplanner.web.users.SettingsController"
title="${i18n:_('User settings')}">
<vbox id="messagesContainer" />
<tabbox>
<tabs>
<tab label="${i18n:_('Data')}" />
</tabs>
<tabpanels>
<tabpanel>
<!-- Data -->
<groupbox style="margin-top: 5px" closable="false">
<hbox>
<label value="${i18n:_('Select language:')}" />
<combobox id="applicationLanguage">
</combobox>
</hbox>
<hbox>
<button label="${i18n:_('Save')}"
sclass="save-button global-action"
onClick="settingsController.save()" />
</hbox>
</groupbox>
</tabpanel>
</tabpanels>
</tabbox>
</window>
</zk>