)
)
public long save(SecurityProfile securityProfile) throws IOException {
SimpleJdbcInsert insert = new SimpleJdbcInsert(template)
.withTableName(TABLE_NAME);
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("allowAllInternal", securityProfile.isAllowAllInternal());
if (securityProfile.getOwner() != null) {
params.addValue("owner_userId", securityProfile.getOwner().getUserId());
}
//if a profile already exists then delete all the old rows first, and repopulate.
//easier than trying to work out which rows need to be updated and which don't
if(securityProfile.getProfileId() != SecurityProfile.UNSAVED_ID) {
MapSqlParameterSource delparams = new MapSqlParameterSource();
delparams.addValue("profileId", securityProfile.getProfileId());
NamedParameterJdbcTemplate namedTemplate = new NamedParameterJdbcTemplate(template);
namedTemplate.update(PROFILE_USERS_GROUPS_DELETE, delparams);
List<SecurityProfile> results = template.query(PROFILE_SELECT_BY_ID, new Object[]{securityProfile.getProfileId()}, new SecurityProfileMapper());
if (results.size() > 0) {
log.error("SecurityProfile deletion failed!");
}
else {
params.addValue("profileId", securityProfile.getProfileId());
insert.execute(params);
}
}
else {
insert.usingGeneratedKeyColumns("profileId");
Number newId = insert.executeAndReturnKey(params);
securityProfile.setProfileId(newId.longValue());
}
//profile read users
if (securityProfile.getReadUsers() != null && !securityProfile.getReadUsers().isEmpty()) {
SimpleJdbcInsert uInsert = new SimpleJdbcInsert(template)
.withTableName("SecurityProfile_ReadUser");
for (User u : securityProfile.getReadUsers()) {
MapSqlParameterSource uParams = new MapSqlParameterSource();
uParams.addValue("SecurityProfile_profileId", securityProfile.getProfileId())
.addValue("readUser_userId", u.getUserId());
uInsert.execute(uParams);
}
}
//profile write users
if (securityProfile.getWriteUsers() != null && !securityProfile.getWriteUsers().isEmpty()) {
SimpleJdbcInsert uInsert = new SimpleJdbcInsert(template)
.withTableName("SecurityProfile_WriteUser");
for (User u : securityProfile.getWriteUsers()) {
MapSqlParameterSource uParams = new MapSqlParameterSource();
uParams.addValue("SecurityProfile_profileId", securityProfile.getProfileId())
.addValue("writeUser_userId", u.getUserId());
uInsert.execute(uParams);
}
}
//profile read groups
if (securityProfile.getReadGroups() != null && !securityProfile.getReadGroups().isEmpty()) {
SimpleJdbcInsert uInsert = new SimpleJdbcInsert(template)
.withTableName("SecurityProfile_ReadGroup");
for (Group g : securityProfile.getReadGroups()) {
MapSqlParameterSource uParams = new MapSqlParameterSource();
uParams.addValue("SecurityProfile_profileId", securityProfile.getProfileId())
.addValue("readGroup_groupId", g.getGroupId());
uInsert.execute(uParams);
}
}
//profile read groups
if (securityProfile.getWriteGroups() != null && !securityProfile.getWriteGroups().isEmpty()) {
SimpleJdbcInsert uInsert = new SimpleJdbcInsert(template)
.withTableName("SecurityProfile_WriteGroup");
for (Group g : securityProfile.getWriteGroups()) {
MapSqlParameterSource uParams = new MapSqlParameterSource();
uParams.addValue("SecurityProfile_profileId", securityProfile.getProfileId())
.addValue("writeGroup_groupId", g.getGroupId());
uInsert.execute(uParams);
}
}