@Transactional
public boolean addDesiredConfig(long clusterId, boolean selected, String user, Config config) {
if (null == user)
throw new NullPointerException("User must be specified.");
HostConfigMapping exist = getDesiredConfigEntity(clusterId, config.getType());
if (null != exist && exist.getVersion().equals(config.getVersionTag())) {
if (!selected) {
exist.setSelected(0);
hostConfigMappingDAO.merge(exist);
}
return false;
}
writeLock.lock();
try {
// set all old mappings for this type to empty
for (HostConfigMapping e : hostConfigMappingDAO.findByType(clusterId,
hostEntity.getHostName(), config.getType())) {
e.setSelected(0);
hostConfigMappingDAO.merge(e);
}
HostConfigMapping hostConfigMapping = new HostConfigMappingImpl();
hostConfigMapping.setClusterId(Long.valueOf(clusterId));
hostConfigMapping.setCreateTimestamp(Long.valueOf(System.currentTimeMillis()));
hostConfigMapping.setHostName(hostEntity.getHostName());
hostConfigMapping.setSelected(1);
hostConfigMapping.setUser(user);
hostConfigMapping.setType(config.getType());
hostConfigMapping.setVersion(config.getVersionTag());
hostConfigMappingDAO.create(hostConfigMapping);
}
finally {
writeLock.unlock();