/**
* Copyright (C) 2012 - 2013, Grass CRM Studio
*
* 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.gcrm.action.system;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.supercsv.io.CsvListReader;
import org.supercsv.io.CsvMapWriter;
import org.supercsv.io.ICsvMapWriter;
import org.supercsv.prefs.CsvPreference;
import com.gcrm.action.crm.BaseListAction;
import com.gcrm.domain.Role;
import com.gcrm.domain.User;
import com.gcrm.exception.ServiceException;
import com.gcrm.service.IBaseService;
import com.gcrm.util.CommonUtil;
import com.gcrm.util.Constant;
import com.gcrm.util.security.UserUtil;
import com.gcrm.vo.SearchCondition;
import com.gcrm.vo.SearchResult;
/**
* Lists Role
*
*/
public class ListRoleAction extends BaseListAction {
private static final long serialVersionUID = -2404576552417042445L;
private IBaseService<Role> baseService;
private IBaseService<User> userService;
private Role role;
private Integer id;
private static final String CLAZZ = Role.class.getSimpleName();
/**
* Gets the list data.
*
* @return null
*/
@Override
public String list() throws Exception {
SearchCondition searchCondition = getSearchCondition();
SearchResult<Role> result = baseService.getPaginationObjects(CLAZZ,
searchCondition);
Iterator<Role> roles = result.getResult().iterator();
long totalRecords = result.getTotalRecords();
getListJson(roles, totalRecords, null, false);
return null;
}
/**
* Gets the list data.
*
* @return null
*/
public String listFull() throws Exception {
UserUtil.permissionCheck("view_system");
Map<String, String> fieldTypeMap = new HashMap<String, String>();
fieldTypeMap.put("created_on", Constant.DATA_TYPE_DATETIME);
fieldTypeMap.put("updated_on", Constant.DATA_TYPE_DATETIME);
User loginUser = UserUtil.getLoginUser();
SearchCondition searchCondition = getSearchCondition(fieldTypeMap,
loginUser.getScope_system(), loginUser);
SearchResult<Role> result = baseService.getPaginationObjects(CLAZZ,
searchCondition);
Iterator<Role> roles = result.getResult().iterator();
long totalRecords = result.getTotalRecords();
getListJson(roles, totalRecords, searchCondition, true);
return null;
}
/**
* Gets the list JSON data.
*
* @return list JSON data
*/
public static void getListJson(Iterator<Role> roles, long totalRecords,
SearchCondition searchCondition, boolean isList) throws Exception {
StringBuilder jsonBuilder = new StringBuilder("");
jsonBuilder
.append(getJsonHeader(totalRecords, searchCondition, isList));
while (roles.hasNext()) {
Role instance = roles.next();
int id = instance.getId();
String name = CommonUtil.fromNullToEmpty(instance.getName());
String description = CommonUtil.fromNullToEmpty(instance
.getDescription());
if (isList) {
User createdBy = instance.getCreated_by();
String createdByName = "";
if (createdBy != null) {
createdByName = CommonUtil.fromNullToEmpty(createdBy
.getName());
}
User updatedBy = instance.getUpdated_by();
String updatedByName = "";
if (updatedBy != null) {
updatedByName = CommonUtil.fromNullToEmpty(updatedBy
.getName());
}
SimpleDateFormat dateFormat = new SimpleDateFormat(
Constant.DATE_TIME_FORMAT);
Date createdOn = instance.getCreated_on();
String createdOnName = "";
if (createdOn != null) {
createdOnName = dateFormat.format(createdOn);
}
Date updatedOn = instance.getUpdated_on();
String updatedOnName = "";
if (updatedOn != null) {
updatedOnName = dateFormat.format(updatedOn);
}
jsonBuilder.append("{\"cell\":[\"").append(id).append("\",\"")
.append(name).append("\",\"").append(description)
.append("\",\"").append(createdByName).append("\",\"")
.append(updatedByName).append("\",\"")
.append(createdOnName).append("\",\"")
.append(updatedOnName).append("\"]}");
} else {
jsonBuilder.append("{\"id\":\"").append(id)
.append("\",\"name\":\"").append(name)
.append("\",\"description\":\"").append(description)
.append("\"}");
}
if (roles.hasNext()) {
jsonBuilder.append(",");
}
}
jsonBuilder.append("]}");
// Returns JSON data back to page
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(jsonBuilder.toString());
}
/**
* Deletes the selected entities.
*
* @return the SUCCESS result
*/
public String delete() throws Exception {
UserUtil.permissionCheck("delete_system");
baseService.batchDeleteEntity(Role.class, this.getSeleteIDs());
return SUCCESS;
}
/**
* Copies the selected entities
*
* @return the SUCCESS result
*/
public String copy() throws Exception {
UserUtil.permissionCheck("create_system");
if (this.getSeleteIDs() != null) {
String[] ids = seleteIDs.split(",");
for (int i = 0; i < ids.length; i++) {
String copyid = ids[i];
Role oriRecord = baseService.getEntityById(Role.class,
Integer.valueOf(copyid));
Role targetRecord = oriRecord.clone();
targetRecord.setId(null);
this.baseService.makePersistent(targetRecord);
}
}
return SUCCESS;
}
/**
* Exports the entities
*
* @return the exported entities inputStream
*/
public InputStream getInputStream() throws Exception {
return getDownloadContent(false);
}
/**
* Exports the template
*
* @return the exported template inputStream
*/
public InputStream getTemplateStream() throws Exception {
return getDownloadContent(true);
}
private InputStream getDownloadContent(boolean isTemplate) throws Exception {
UserUtil.permissionCheck("view_system");
ResourceBundle rb = CommonUtil.getResourceBundle();
String fileName = rb.getString("entity.role.label") + ".csv";
fileName = new String(fileName.getBytes(), "ISO8859-1");
File file = new File(fileName);
ICsvMapWriter writer = new CsvMapWriter(new FileWriter(file),
CsvPreference.EXCEL_PREFERENCE);
try {
final String[] header = new String[] {
rb.getString("entity.id.label"),
rb.getString("entity.name.label"),
rb.getString("entity.description.label"),
rb.getString("entity.notes.label") };
writer.writeHeader(header);
if (!isTemplate) {
String[] ids = seleteIDs.split(",");
for (int i = 0; i < ids.length; i++) {
String id = ids[i];
Role role = baseService.getEntityById(Role.class,
Integer.parseInt(id));
final HashMap<String, ? super Object> data1 = new HashMap<String, Object>();
data1.put(header[0], role.getId());
data1.put(header[1],
CommonUtil.fromNullToEmpty(role.getName()));
data1.put(header[2],
CommonUtil.fromNullToEmpty(role.getDescription()));
data1.put(header[3],
CommonUtil.fromNullToEmpty(role.getNotes()));
writer.write(data1, header);
}
}
} catch (Exception e) {
throw e;
} finally {
writer.close();
}
InputStream in = new FileInputStream(file);
this.setFileName(fileName);
return in;
}
/**
* Imports the entities
*
* @return the SUCCESS result
*/
public String importCSV() throws Exception {
File file = this.getUpload();
CsvListReader reader = new CsvListReader(new FileReader(file),
CsvPreference.EXCEL_PREFERENCE);
ResourceBundle rb = CommonUtil.getResourceBundle();
int failedNum = 0;
int successfulNum = 0;
try {
final String[] header = reader.getCSVHeader(true);
List<String> line = new ArrayList<String>();
Map<String, String> failedMsg = new HashMap<String, String>();
while ((line = reader.read()) != null) {
Map<String, String> row = new HashMap<String, String>();
for (int i = 0; i < line.size(); i++) {
row.put(header[i], line.get(i));
}
Role role = new Role();
try {
String id = row.get(rb.getString("entity.id.label"));
if (!CommonUtil.isNullOrEmpty(id)) {
role.setId(Integer.parseInt(id));
}
role.setName(CommonUtil.fromNullToEmpty(row.get(rb
.getString("entity.name.label"))));
role.setDescription(CommonUtil.fromNullToEmpty(row.get(rb
.getString("entity.description.label"))));
role.setNotes(CommonUtil.fromNullToEmpty(row.get(rb
.getString("entity.notes.label"))));
baseService.makePersistent(role);
successfulNum++;
} catch (Exception e) {
failedNum++;
failedMsg.put(role.getName(), e.getMessage());
}
}
this.setFailedMsg(failedMsg);
this.setFailedNum(failedNum);
this.setSuccessfulNum(successfulNum);
this.setTotalNum(successfulNum + failedNum);
} finally {
reader.close();
}
return SUCCESS;
}
/**
* Selects the entities
*
* @return the SUCCESS result
*/
public String select() throws ServiceException {
User user = null;
Set<Role> roles = null;
if ("User".equals(this.getRelationKey())) {
user = userService.getEntityById(User.class,
Integer.valueOf(this.getRelationValue()));
roles = user.getRoles();
}
if (this.getSeleteIDs() != null) {
String[] ids = seleteIDs.split(",");
for (int i = 0; i < ids.length; i++) {
String selectId = ids[i];
role = baseService.getEntityById(Role.class,
Integer.valueOf(selectId));
roles.add(role);
}
}
if ("User".equals(this.getRelationKey())) {
userService.makePersistent(user);
}
return SUCCESS;
}
/**
* Unselects the entities
*
* @return the SUCCESS result
*/
public String unselect() throws ServiceException {
User user = null;
Set<Role> roles = null;
if ("User".equals(this.getRelationKey())) {
user = userService.getEntityById(User.class,
Integer.valueOf(this.getRelationValue()));
roles = user.getRoles();
}
if (this.getSeleteIDs() != null) {
String[] ids = seleteIDs.split(",");
Collection<Role> selectedRoles = new ArrayList<Role>();
for (int i = 0; i < ids.length; i++) {
Integer removeId = Integer.valueOf(ids[i]);
A: for (Role role : roles) {
if (role.getId().intValue() == removeId.intValue()) {
selectedRoles.add(role);
break A;
}
}
}
roles.removeAll(selectedRoles);
}
if ("User".equals(super.getRelationKey())) {
userService.makePersistent(user);
}
return SUCCESS;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
@Override
protected String getEntityName() {
return Role.class.getSimpleName();
}
/**
* @return the baseService
*/
public IBaseService<Role> getBaseService() {
return baseService;
}
/**
* @param baseService
* the baseService to set
*/
public void setBaseService(IBaseService<Role> baseService) {
this.baseService = baseService;
}
/**
* @return the role
*/
public Role getRole() {
return role;
}
/**
* @param role
* the role to set
*/
public void setRole(Role role) {
this.role = role;
}
/**
* @return the id
*/
@Override
public Integer getId() {
return id;
}
/**
* @param id
* the id to set
*/
@Override
public void setId(Integer id) {
this.id = id;
}
/**
* @return the userService
*/
public IBaseService<User> getUserService() {
return userService;
}
/**
* @param userService
* the userService to set
*/
public void setUserService(IBaseService<User> userService) {
this.userService = userService;
}
}