Skip to content

Instantly share code, notes, and snippets.

@atishn
Created December 26, 2016 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atishn/3521da8a9183ef5d74dd07b8f52bdb8b to your computer and use it in GitHub Desktop.
Save atishn/3521da8a9183ef5d74dd07b8f52bdb8b to your computer and use it in GitHub Desktop.
@Component
@Service
public class SimpleDepartmentService implements DepartmentService {
/**
* Cache service.
*/
@Reference
private CacheService cacheService;
/**
* Creates a Department with the information in the given node.
*
* @param node
*
* @return
*
* @throws RepositoryException
*/
@Override
public Department get(final Node node) throws RepositoryException {
String cacheId = "get" + node.getPath();
Option<Department> option = getFromCache(cacheId, Department.class);
if (option.isDefined()) {
return option.get();
} else {
return createDepartment(node, cacheId);
}
}
private Department createDepartment(final Node node, final String cacheId) throws
RepositoryException {
Property businessUnit = node.getProperty("Business Unit");
Property division = node.getProperty("Division");
PropertyIterator idIterator = node.getProperties("ID");
List<String> id = new ArrayList<>();
while (idIterator.hasNext()) {
id.add(idIterator.nextProperty().getString());
}
PropertyIterator nameIterator = node.getProperties("Names");
List<String> names = new ArrayList<>();
while (nameIterator.hasNext()) {
names.add(nameIterator.nextProperty().getString());
}
Department department = new Department(businessUnit.getString(), division.getString(), id, names);
addToCache(cacheId, department);
return department;
}
/**
* Get from Department's cache.
* @param itemId Item identifier
* @return an Optional value
*/
public <T> Option<T> getFromCache(final String itemId, final Class<T> expectedType) {
Object item = cacheService.getFromCache(CacheServiceImpl.CACHE_DEPARTMENTS,
itemId);
return Option.of(item).filter(expectedType::isInstance).map(expectedType::cast);
}
/**
* Add to Department's cache.
* @param itemId Item identifier
* @param item item to be stored
*/
public void addToCache(final String itemId, final Object item) {
cacheService.addToCache(CacheServiceImpl.CACHE_DEPARTMENTS, itemId, item);
}
public List<OtherLocationDTO> getOtherDepartments(final JspContext jspContext,
final String depId) {
return Try.of( () -> Contexts
.withReadPrivilege(resolverFactory, (session,
resourceResolver) -> {
UserInfoService userInfoService = Pages.getService(jspContext, UserInfoService.class);
MyDepartmentDTO departmentDTO = userInfoService.obtainUserDepartmentInfo(depId);
return Nodes.getOtherLocations("/content/mykohls/departments",
StringUtils.EMPTY,
session, getPredicate(departmentDTO), Comparator.comparing(OtherLocationDTO::getTitle));
})).getOrElse(Collections.emptyList());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment