// TODO: move the actual work to some other class not in rest package..
// we have access carry on...
FileShareAnswer fsa = new FileShareAnswer();
FileBean fileBean = fsr.getFile();
// ObjectMapper mapper = new ObjectMapper(); // can reuse, share
// // globally
// Map<String, String> json = null;
// try {
// json = mapper.readValue(awr.getJson(),
// // new TypeReference<Map<String, Object>>() {
// new TypeReference<Map<String, String>>() {
// });
//
// mapper.readValue(node.get("vendor").getTextValue(), Vendor.class);
//
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// String fileJson = (String) json.get("file");
// String fileShareUsersJson = (String) json.get("fileShareUsers");
//
// // FileBean fileBean = (FileBean) json.get("file");
// FileBean fileBean = null;
// Collection<String> fileShareUsers = null;
// // try {
// // fileBean = C((Map)json.get("file"),new TypeReference<FileBean>() {
// // } );
// // fileShareUsers =
// mapper.readValue((String)json.get("fileShareUsers"),
// // new TypeReference<Collection<String>>() {
// // } );
// // } catch (IOException e1) {
// // // TODO Auto-generated catch block
// // e1.printStackTrace();
// // }
Collection<String> fileShareUsers = fsr.getFileShareUsers();
// Collection<String> fileShareUsers = (Collection<String>)
// json.get("fileShareUsers");
fsa.setFile(fileBean);
Connection con = null;
try {
con = DbHandler.getConnection();
con.setAutoCommit(false);
fileBean.setUpdatedby(userId);
fileBean.setTsupdated(new Timestamp(System.currentTimeMillis()));
fileBean.store(con);
// service groups
Collection<FileshareuserBean> col = FileshareuserFinder
.findByFileid(con, fileBean.getId());
// loop exsisting FileshareuserBean update or remove based on
// status
for (FileshareuserBean fsuBean : col) {
if (!fileShareUsers.contains(fsuBean.getUserid())) {
fsuBean.remove(con);
}
fileShareUsers.remove(fsuBean.getUserid());
}
User fileUser;
// loop remaining services and create them
for (String uId : fileShareUsers) {
FileshareuserBean fsuBean = new FileshareuserBean();
fsuBean.setFileid(fileBean.getId());
fsuBean.setUserid(uId);
fileUser = (User) UserFinderBase.findByPrimaryKey(new UserKey(
uId));
if (null != fileUser) {
// set correct label ending up on switch
fsuBean.setUserdescription(fileUser.getFirstname() + " "
+ fileUser.getLastname());
}
fsuBean.create(con);
}
con.commit();
fileBean.setFileShareUserCol(FileshareuserFinder
.findByFileid(fileBean.getId()));
} catch (SQLException sqle) {
try {
if (null != con) {
con.rollback();
}
} catch (SQLException e) {
// getLog().log(Level.ERROR,
// "createData() error doing rollback: ", e);
}
} finally {
if (null != con) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
con = null;
}
}
rw.setLinks(getLinksForObject(userId, fileBean.getId().toString()));
rw.setSuccess(true);
rw.setData(fsa);
return Response.status(Response.Status.OK).entity(rw).build();