package com.mr.webiter;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.mr.dao.ObjectDao;
import com.mr.model.ArticleInfo;
import com.mr.model.Info;
import com.mr.model.PhotoInfo;
import com.mr.model.ReArticleInfo;
import com.mr.model.UserInfo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserInfoAction extends ActionSupport implements ModelDriven<UserInfo>, ServletRequestAware{
private static final long serialVersionUID = 3496418228419922282L;
private UserInfo userInfo = new UserInfo();
private String hql = "";
private ObjectDao<UserInfo> objectDao = null;
protected HttpServletRequest request;
public UserInfo getModel() {
return userInfo;
}
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
// 删除朋友信息
public String friend_delete() {
String friendsTypes[] = request.getParameterValues("friends");
if (friendsTypes != null) {
String account = (String) request.getSession().getAttribute(
"account");
hql = "from UserInfo where account = '" + account + "'";
objectDao = new ObjectDao<UserInfo>();
UserInfo user = objectDao.queryFrom(hql);
String date_types = user.getFriendType();
for (String type : friendsTypes) {
date_types = com.mr.tools.ValidateExpression.deleteString(
date_types, type);
}
user.setFriendType(date_types);
objectDao.updateT(user);
}
return friend_query();
}
// 后台查询用户
public String friend_query() {
String account = "";
// try {
account = (String) request.getSession().getAttribute("account");
//account = new String(account.getBytes("ISO8859_1"), "gb2312");
System.out.println("AAAAAAAAaa "+account);
// } catch (UnsupportedEncodingException e) {
// e.printStackTrace();
// }
hql = "from UserInfo where account = '" + account + "'";
System.out.println("HQL :"+hql);
objectDao = new ObjectDao<UserInfo>();
userInfo = objectDao.queryFrom(hql);
String friend = userInfo.getFriendType();
if (!friend.equals("")) {
request.setAttribute("friends", friend.split(","));
}
String[] ff = friend.split(",");
System.out.println("FFFFFFFFFFf "+ff.length);
return "friend_query";
}
// 添加好友
public String friend_add() {
boolean flag = true;
objectDao = new ObjectDao<UserInfo>();
String account = (String) request.getSession().getAttribute("account");
UserInfo user = objectDao.queryFrom("from UserInfo where account = '"
+ account + "'");
String friends = user.getFriendType();
System.out.println("Friends "+friends);
UserInfo userInfo = (UserInfo) request.getSession().getAttribute(
"userInfo");
String account1 = userInfo.getAccount(); // 获取访问博客的用户名
if (friends == null || friends.equals("")) {
user.setFriendType(account1);
} else {
String types[] = friends.split(",");
for (String type1 : types) {
if (account1.equals(type1)) {
flag = false;
}
}
user.setFriendType(user.getFriendType() + "," + account1);
}
String result = "该好友已经存在,操作失败!";
if (flag) {
objectDao = new ObjectDao<UserInfo>();
objectDao.updateT(user);
System.out.println("AFTER "+ userInfo.getFriendType() + "," + account1);
result = "加入好友成功!";
}
request.setAttribute("account1", account1);
request.setAttribute("sign", "18");
request.setAttribute("result", result);
return "operationUser";
}
// 删除相册类别操作
public String phtType_delete() {
String phtTypes[] = request.getParameterValues("type");
if (phtTypes != null) {
String account = (String) request.getSession().getAttribute(
"account");
hql = "from UserInfo where account = '" + account + "'";
objectDao = new ObjectDao<UserInfo>();
UserInfo user = objectDao.queryFrom(hql);
String date_phtType = user.getPicType();
for (String phtType : phtTypes) {
date_phtType = com.mr.tools.ValidateExpression.deleteString(
date_phtType, phtType);
hql = "from PhotoInfo where author = '" + account
+ "' and photoType = '" + phtType + "'";
ObjectDao<PhotoInfo> dao = new ObjectDao<PhotoInfo>();
List<PhotoInfo> list = dao.queryList(hql);
for (PhotoInfo photoInfo : list) {
dao.deleteT(photoInfo);
String path = request.getRealPath("/"+ photoInfo.getPhotoAddress());
File file = new File(path);
if (file.exists()) {
file.delete();
}
}
}
user.setPicType(date_phtType);
objectDao.updateT(user);
}
request.setAttribute("sign", "13");
return "operationUser";
}
// 修改相片类别名称
public String phtType_update() {
String oldType = request.getParameter("oldType");
String newsType = request.getParameter("newsType");
String account = (String) request.getSession().getAttribute("account");
hql = "from UserInfo where account = '" + account + "'";
objectDao = new ObjectDao<UserInfo>();
UserInfo user = objectDao.queryFrom(hql);
String date_phtType = user.getPicType();
String arrays[] = date_phtType.split(",");
boolean flag = true;
for (String array : arrays) {
if (array.equals(newsType)) {
flag = false;
}
}
if (flag) {
if (!newsType.equals("")) {
date_phtType = com.mr.tools.ValidateExpression.deleteString(
date_phtType, oldType);
date_phtType = date_phtType.trim();
if (date_phtType.equals("")) {
date_phtType = newsType;
} else {
date_phtType = date_phtType + "," + newsType;
}
user.setPicType(date_phtType);
objectDao.updateT(user);
hql = "from PhotoInfo where author = '" + account
+ "' and photoType = '" + oldType + "'";
ObjectDao<PhotoInfo> dao = new ObjectDao<PhotoInfo>();
List<PhotoInfo> list = dao.queryList(hql);
for (PhotoInfo photoInfo : list) {
photoInfo.setPhotoType(newsType);
System.out.println(newsType);
dao.updateT(photoInfo);
}
}
}
request.setAttribute("sign", "14");
return "operationUser";
}
// 查询并添加相片类别信息
public String phtType_query() {
boolean flag = true;
String account = (String) request.getSession().getAttribute("account");
hql = "from UserInfo where account = '" + account + "'";
objectDao = new ObjectDao<UserInfo>();
UserInfo user = objectDao.queryFrom(hql);
String pht_type = user.getPicType();
// 添加类别
if (!userInfo.getPicType().equals("")) {
String arrays[] = pht_type.split(",");
for (String array : arrays) {
if (array.equals(userInfo.getPicType())) {
this.addFieldError("pht_type", "您输入的相册类别重复,请重新输入!");
flag = false;
}
}
if (null == pht_type || pht_type.equals("")) {
user.setPicType(userInfo.getPicType());
} else {
user
.setPicType(user.getPicType() + ","
+ userInfo.getPicType());
}
if (flag) {
objectDao.updateT(user);
user = objectDao.queryFrom(hql);
pht_type = user.getPicType();
}
}
// 查询类别
pht_type = pht_type.trim();
if (!pht_type.equals("")) {
String pht_types[] = pht_type.split(",");
request.setAttribute("pht_types", pht_types);
}
return SUCCESS;
}
// 转向添加文章的页面,这里需要将文章的类别转换成数组
public String forwardAddArticle() {
String account = (String) request.getSession().getAttribute("account");
hql = "from UserInfo where account = '" + account + "'";
objectDao = new ObjectDao<UserInfo>();
UserInfo userInfo = (UserInfo) objectDao.queryFrom(hql);
String type = userInfo.getArtType();
String types[] = type.split(",");
request.setAttribute("types", types);
return "forwardAddArticle";
}
// 修改文章类别名称
public String artType_update() {
String oldType = request.getParameter("oldType");
String newsType = request.getParameter("newsType");
String account = (String) reque