Created
September 30, 2014 10:21
-
-
Save ramrrr/076da4ff15fff95bbe0c to your computer and use it in GitHub Desktop.
SMSRepo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package heimdall.channel.sms; | |
| import edu.umd.cs.findbugs.annotations.CheckForNull; | |
| import edu.umd.cs.findbugs.annotations.NonNull; | |
| import edu.umd.cs.findbugs.annotations.Nullable; | |
| import heimdall.auth.AbstractOrganizationScopedRepository; | |
| import heimdall.auth.Organization; | |
| import heimdall.auth.UserContext; | |
| import heimdall.core.Id; | |
| import org.springframework.context.MessageSource; | |
| import javax.inject.Inject; | |
| import java.util.List; | |
| import java.util.Locale; | |
| /** | |
| * SmsChannelConfigRepositoryImpl. | |
| * @author srinivasulug | |
| * | |
| */ | |
| public class SmsChannelConfigRepositoryImpl | |
| extends | |
| AbstractOrganizationScopedRepository<SmsChannelConfig, SmsChannelConfigRepository.SmsChannelConfigQueryBuilder> | |
| implements | |
| SmsChannelConfigRepository { | |
| private static final String DEFAULT_CONFIG_KEY = SmsChannelConfig.class.getName() + ".default"; | |
| private MessageSource messageSource; | |
| /** | |
| * Constructor with User context. | |
| * @param userContext User context | |
| */ | |
| public SmsChannelConfigRepositoryImpl(final UserContext userContext) { | |
| super(userContext); | |
| } | |
| @Inject | |
| public void setMessageSource(final MessageSource messageSource) { | |
| this.messageSource = messageSource; | |
| } | |
| @Override | |
| public Class<SmsChannelConfig> getEntityClass() { | |
| return SmsChannelConfig.class; | |
| } | |
| @Override | |
| public SmsChannelConfig newInstance() { | |
| return new SmsChannelConfig(getUserContext()); | |
| } | |
| @Override | |
| public SmsChannelConfigQueryBuilder query() { | |
| return new SmsChannelConfigQueryBuilderImpl(getEntityManager(), getUserContext().getOrganization()); | |
| } | |
| @Override | |
| public SmsChannelConfig getDefault() { | |
| String defaultSmsChannelConfigId = getUserContext().getOrganization().getSetting(DEFAULT_CONFIG_KEY); | |
| return getById(Id.valueOf(defaultSmsChannelConfigId)); | |
| } | |
| @Override | |
| public void setDefault(@NonNull final Organization organization, | |
| @Nullable final SmsChannelConfig smsChannelConfig) { | |
| final String defaultValue = (smsChannelConfig == null) ? null : smsChannelConfig.getId().toString(); | |
| organization.setSetting(DEFAULT_CONFIG_KEY, defaultValue); | |
| super.saveEntity(organization); | |
| } | |
| @Override | |
| @CheckForNull | |
| public SmsChannelConfig getByName(final String name) { | |
| return query().getByConfigName(name).first(); | |
| } | |
| @Override | |
| @CheckForNull | |
| public SmsChannelConfig getByNameForOwner(final String channelName, final Organization owner) { | |
| return query().getByConfigName(channelName).ownedBy(owner).first(); | |
| } | |
| @Override | |
| public SmsChannelConfig save(final SmsChannelConfig entity) { | |
| return super.save(entity); | |
| } | |
| @Override | |
| public boolean canDelete(final SmsChannelConfig enity) { | |
| return getDependencyDescription(enity).isDeleteAllowed(); | |
| } | |
| @Override | |
| public SmsChannelConfigRepository.DependencyDescription getDependencyDescription( | |
| final SmsChannelConfig entity) { | |
| return new SmsChannelConfigRepository.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 SmsChannelConfig getEntity() { | |
| return entity; | |
| } | |
| }; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment