org.ff4j.services.FeatureStoreServices.kt Maven / Gradle / Ivy
/*-
* #%L
* ff4j-spring-services
* %%
* Copyright (C) 2013 - 2019 FF4J
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.ff4j.services
import org.apache.commons.lang3.StringUtils
import org.ff4j.FF4j
import org.ff4j.cache.FF4jCacheProxy
import org.ff4j.core.FeatureStore
import org.ff4j.services.domain.CacheApiBean
import org.ff4j.services.domain.FeatureApiBean
import org.ff4j.services.domain.FeatureStoreApiBean
import org.ff4j.services.domain.GroupDescApiBean
import org.ff4j.services.exceptions.FeatureStoreNotCached
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.util.CollectionUtils
import java.util.stream.Collectors
/**
* Created by Paul
*
* @author Paul Williams
*/
@Service
class FeatureStoreServices(@Autowired val fF4j: FF4j) {
fun getFeatureStore(): FeatureStoreApiBean {
return FeatureStoreApiBean(this.fF4j.featureStore)
}
fun getAllFeatures(): Collection {
val allFeatures = fF4j.featureStore.readAll()
return if (CollectionUtils.isEmpty(allFeatures)) {
ArrayList(0)
} else {
val features = ArrayList(allFeatures.size)
features.addAll(allFeatures.values.stream().map { FeatureApiBean(it) }.collect(Collectors.toList()))
features
}
}
fun getAllGroups(): MutableCollection {
val groups = HashMap()
val allFeatures = fF4j.featureStore.readAll()
allFeatures?.let {
allFeatures.values.forEach {
initGroup(groups, it.uid, it.group)
}
}
return groups.values
}
private fun initGroup(groups: HashMap, uid: String, groupName: String?) {
groupName?.let {
if (StringUtils.isNotBlank(groupName)) {
if (!groups.containsKey(groupName)) {
groups[groupName] = GroupDescApiBean(groupName, ArrayList())
}
groups[groupName]?.features?.add(uid)
}
}
}
fun deleteAllFeatures() {
fF4j.featureStore.clear()
}
fun getFeaturesFromCache(): CacheApiBean {
fF4j.cacheProxy ?: throw FeatureStoreNotCached()
return CacheApiBean(fF4j.featureStore)
}
fun clearCachedFeatureStore() {
// Fixing #218 : If audit is enabled, cannot clear cache.
val cacheProxy = fF4j.cacheProxy ?: throw FeatureStoreNotCached()
cacheProxy.cacheManager.clearFeatures()
}
}