📄 updateaccountaction.java
字号:
/*
* Copyright 2006 Borys Burnayev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rdacorp.petstore.web.action;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import com.rdacorp.petstore.domain.Account;
import com.rdacorp.petstore.domain.Address;
import com.rdacorp.petstore.domain.Category;
import com.rdacorp.petstore.domain.Language;
import com.rdacorp.petstore.domain.Person;
import com.rdacorp.petstore.domain.State;
import com.rdacorp.petstore.domain.UserProfile;
/**
* @author Borys Burnayev
*/
public class UpdateAccountAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
PetStoreApplication application = new PetStoreApplication(request.getSession().getServletContext());
PetStoreSession session = new PetStoreSession(request.getSession());
MaintainAccountForm frm = (MaintainAccountForm) form;
Account account = session.getAccount();
Category favoriteCategory = null;
Category category = Category.findByID(application.getCategories(), frm.getFavoriteCategory());
Language language = Language.findByID(application.getLanguages(), frm.getPreferredLanguage());
State state = State.findByID(application.getStates(), frm.getState());
if (account != null) {
Person person = account.getPerson();
Address address = account.getAddress();
UserProfile profile = person.getUserProfile();
person.setFirstName(frm.getFirstName());
person.setLastName(frm.getLastName());
person.setPhoneNumber(new PhoneNumber(frm.getPhone()).getPhone());
person.setEnableMylist(frm.getEnableMyList() ? 1 : 0);
person.setEnableTips(frm.getEnableTips() ? 1 : 0);
person.setFavoriteCategory(category);
person.setPreferredLanguage(language);
address.setAddressLine1(frm.getStreetAddress1());
address.setAddressLine2(frm.getStreetAddress2());
address.setCity(frm.getCity());
address.setZip(new Zip(frm.getZip()).toString());
address.setState(state);
profile.setPassword(frm.getPassword());
Account updatedAccount = application.getPetStoreService().updateAccount(account);
session.setAccount(updatedAccount);
favoriteCategory = updatedAccount.getPerson().getFavoriteCategory();
}
else {
// check if the email is already on file
String email = frm.getEmail();
Account existingAccount = application.getPetStoreService().findAccount(email);
if (existingAccount != null) {
ActionMessages errors = new ActionMessages();
errors.add("email", new ActionMessage("error.login.already.exists"));
saveErrors(request, errors);
return mapping.findForward("error");
}
Person person = new Person(frm.getFirstName(), frm.getLastName(), frm.getEmail(), new PhoneNumber(frm
.getPhone()).getPhone(), (frm.getEnableMyList() ? 1 : 0), (frm.getEnableTips() ? 1 : 0), category,
language);
Address address = new Address(frm.getStreetAddress1(), frm.getStreetAddress2(), frm.getCity(), new Zip(frm
.getZip()).getZip(), state);
Account newAccount = new Account(person, address);
UserProfile profile = new UserProfile(frm.getEmail(), frm.getPassword(), person);
profile.setPerson(person);
Account createdAccount = application.getPetStoreService().createAccount(newAccount);
session.setAccount(createdAccount);
favoriteCategory = createdAccount.getPerson().getFavoriteCategory();
}
if (favoriteCategory != null) {
Collection products = application.getPetStoreService().getProducts(favoriteCategory);
session.setProducts(products);
}
return mapping.findForward("success");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -