Skip to content

Instantly share code, notes, and snippets.

@ramrrr
Created September 30, 2014 09:43
Show Gist options
  • Select an option

  • Save ramrrr/68479eab507c6e7f3c26 to your computer and use it in GitHub Desktop.

Select an option

Save ramrrr/68479eab507c6e7f3c26 to your computer and use it in GitHub Desktop.
Voice Channel
package heimdall.channel.voice;
import heimdall.auth.AbstractOrganizationScopedRepository;
import heimdall.auth.Organization;
import heimdall.auth.UserContext;
import heimdall.core.Id;
import java.util.List;
import java.util.Locale;
import javax.inject.Inject;
import org.springframework.context.MessageSource;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
/**
* VoiceChannelConfigRepositoryImpl.
*
* @author srinivasulug
*
*/
public class VoiceChannelConfigRepositoryImpl
extends
AbstractOrganizationScopedRepository<VoiceChannelConfig, VoiceChannelConfigRepository.VoiceChConfigQueryBuilder>
implements VoiceChannelConfigRepository {
private static final String DEFAULT_CONFIG_KEY = VoiceChannelConfig.class
.getName() + ".default";
private MessageSource messageSource;
/**
* Constructor with User context.
*
* @param userContext
* User context
*/
public VoiceChannelConfigRepositoryImpl(final UserContext userContext) {
super(userContext);
}
@Override
public VoiceChannelConfig newInstance() {
return new VoiceChannelConfig(getUserContext());
}
@Inject
public void setMessageSource(final MessageSource messageSource) {
this.messageSource = messageSource;
}
public MessageSource getMessageSource() {
return messageSource;
}
@Override
public Class<VoiceChannelConfig> getEntityClass() {
return VoiceChannelConfig.class;
}
@Override
public VoiceChConfigQueryBuilder query() {
return new VoiceChannelConfigQueryBuilderImpl(getEntityManager(),
getUserContext().getOrganization());
}
@Override
public VoiceChannelConfig getDefault() {
String defaultVoiceChannelConfigId = getUserContext().getOrganization()
.getSetting(DEFAULT_CONFIG_KEY);
return getById(Id.valueOf(defaultVoiceChannelConfigId));
}
@Override
public void setDefault(@NonNull final Organization organization,
@Nullable final VoiceChannelConfig voiceChannelConfig) {
final String defaultValue = (voiceChannelConfig == null) ? null
: voiceChannelConfig.getId().toString();
organization.setSetting(DEFAULT_CONFIG_KEY, defaultValue);
super.saveEntity(organization);
}
@Override
public boolean canDelete(final VoiceChannelConfig enity) {
return getDependencyDescription(enity).isDeleteAllowed();
}
@Override
public DependencyDescription getDependencyDescription(
final VoiceChannelConfig entity) {
return new DependencyDescription() {
@Override
public boolean isDeleteAllowed() {
List<Organization> allChildren = getUserContext().getOrganization().getChildren();
boolean canBeDeleted = true;
if (getDefault() != null
&& getDefault().getId().toString()
.equals(entity.getId().toString())) {
canBeDeleted = false;
}
else{
for (Organization org : allChildren) {
String defaultVoiceChannelConfigId = org.getSetting(DEFAULT_CONFIG_KEY);
if (defaultVoiceChannelConfigId != null
&& defaultVoiceChannelConfigId .equals(entity.getId().toString())){
canBeDeleted = false;
}
}
}
return canBeDeleted;
}
@Override
public String getReason(final Locale locale) {
return messageSource.getMessage("voice.config.cannot.delete",
new Object[0], locale);
}
@Override
public VoiceChannelConfig getEntity() {
return entity;
}
};
}
@Override
@CheckForNull
public VoiceChannelConfig getByName(final String name) {
return query().ignoreVisibility().getByConfigName(name).first();
}
@Override
@CheckForNull
public VoiceChannelConfig getByNameForOwner(final String channelName, final Organization owner) {
return query().getByConfigName(channelName).ownedBy(owner).first();
}
@Override
public VoiceChannelConfig save(final VoiceChannelConfig entity) {
return super.save(entity);
}
@Override
@CheckForNull
public VoiceChannelConfig getByOriginNumber(final String originnumber, final Organization organization) {
return query().visibleTo(organization).getByOriginNumber(originnumber).first();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment