List<SharedItemReff> sharings = null;
// missing shareWith
if (shareWith == null) {
pm.close();
throw new RequestException(ReqErrorTypes.MISSING_ARGUMENT);
}
// not owner
if (!item.getOwnerId().equals(request.getCurrentUserId())) {
pm.close();
throw new RequestException(ReqErrorTypes.UNAUTHORIZED_OPERATION);
}
// can't change owner settings
if (item.getOwnerId().equals(shareWith)) {
pm.close();
throw new RequestException(ReqErrorTypes.UNAUTHORIZED_OPERATION);
}
try {
sharings = item.getSharedItemReff();
int index = 0;
for (SharedItemReff share : sharings) {
if (share.getSharedWithId().equals(shareWith)
&& !share.getSharingType().equals(SharingType.OWNER)) {
shareExists = true;
if (sharingType == null)
remove = index;
else
share.setSharingType(sharingType);
}
index++;
}
// add
if (remove > -1)
item.getSharedItemReff().remove(remove);
// add new sharing
if (!shareExists && request.getSharingType() != null
&& !request.getSharingType().equals(SharingType.OWNER)) {
newShare = new SharedItemReff();
newShare.setSharedWithId(request
.getParameter(ReqParamNames.SHARE_WITH.getParamName()));
newShare.setSharingType(request.getSharingType());
item.addSharedItemReff(newShare);
}
updated(pm, item, request);
} catch (RequestException e) {
throw e;
} catch (Exception e) {
log.warning(e.getMessage());
throw new RequestException(ReqErrorTypes.REQUEST_FAILED);
}
}