use of org.apache.commons.collections.comparators.ComparatorChain in project coprhd-controller by CoprHD.
the class CreationTimeComparator method getComparator.
private ComparatorChain getComparator() {
if (COMPARATOR == null) {
COMPARATOR = new ComparatorChain();
COMPARATOR.addComparator(new BeanComparator(CREATION_TIME, new NullComparator()), reverseOrder);
}
return COMPARATOR;
}
use of org.apache.commons.collections.comparators.ComparatorChain in project carbon-business-process by wso2.
the class ProcessManagementServiceSkeleton method processQuery.
/**
* Query processes based on a {@link org.apache.ode.bpel.common.ProcessFilter} criteria. This is
* implemented in memory rather than via database calls since the processes
* are managed by the {@link org.apache.ode.bpel.iapi.ProcessStore} object and we don't want to make
* this needlessly complicated.
*
* @param filter process filter
* @param tenantsProcessStore Current Tenant's process store
* @return ProcessConf collection
* @throws ProcessManagementException if an error occurred while processing query
*/
private Collection<ProcessConf> processQuery(ProcessFilter filter, TenantProcessStoreImpl tenantsProcessStore) throws ProcessManagementException {
Map<QName, ProcessConfigurationImpl> processes = tenantsProcessStore.getProcessConfigMap();
if (log.isDebugEnabled()) {
for (Map.Entry<QName, ProcessConfigurationImpl> process : processes.entrySet()) {
log.debug("Process " + process.getKey() + " in state " + process.getValue());
}
}
Set<QName> pids = processes.keySet();
// Name filter can be implemented using only the PIDs.
if (filter != null && filter.getNameFilter() != null) {
// adding escape sequences to [\^$.|?*+(){} characters
String nameFilter = filter.getNameFilter().replace("\\", "\\\\").replace("]", "\\]").replace("[", "\\[").replace("^", "\\^").replace("$", "\\$").replace("|", "\\|").replace("?", "\\?").replace(".", "\\.").replace("+", "\\+").replace("(", "\\(").replace(")", "\\)").replace("{", "\\{").replace("}", "\\}").replace("*", ".*");
final Pattern pattern = Pattern.compile(nameFilter + "(-\\d*)?");
CollectionsX.remove_if(pids, new MemberOfFunction<QName>() {
@Override
public boolean isMember(QName o) {
return !pattern.matcher(o.getLocalPart()).matches();
}
});
}
if (filter != null && filter.getNamespaceFilter() != null) {
// adding escape sequences to [\^$.|?*+(){} characters
String namespaceFilter = filter.getNamespaceFilter().replace("\\", "\\\\").replace("]", "\\]").replace("[", "\\[").replace("^", "\\^").replace("$", "\\$").replace("|", "\\|").replace("?", "\\?").replace(".", "\\.").replace("+", "\\+").replace("(", "\\(").replace(")", "\\)").replace("{", "\\{").replace("}", "\\}").replace("*", ".*");
final Pattern pattern = Pattern.compile(namespaceFilter);
CollectionsX.remove_if(pids, new MemberOfFunction<QName>() {
@Override
public boolean isMember(QName o) {
String ns = o.getNamespaceURI() == null ? "" : o.getNamespaceURI();
return !pattern.matcher(ns).matches();
}
});
}
// Now we need the process conf objects, we need to be
// careful since someone could have deleted them by now
List<ProcessConf> confs = new LinkedList<ProcessConf>();
for (QName pid : pids) {
ProcessConf pConf = tenantsProcessStore.getProcessConfiguration(pid);
if (pConf != null) {
confs.add(pConf);
}
}
if (filter != null) {
// Specific filter for deployment date.
if (filter.getDeployedDateFilter() != null) {
for (final String ddf : filter.getDeployedDateFilter()) {
final Date dd;
try {
dd = ISO8601DateParser.parse(Filter.getDateWithoutOp(ddf));
} catch (ParseException e) {
// Should never happen.
String errMsg = "Exception while parsing date";
log.error(errMsg, e);
throw new ProcessManagementException(errMsg, e);
}
CollectionsX.remove_if(confs, new MemberOfFunction<ProcessConf>() {
@Override
public boolean isMember(ProcessConf o) {
if (ddf.startsWith("=")) {
return !o.getDeployDate().equals(dd);
}
if (ddf.startsWith("<=")) {
return o.getDeployDate().getTime() > dd.getTime();
}
if (ddf.startsWith(">=")) {
return o.getDeployDate().getTime() < dd.getTime();
}
if (ddf.startsWith("<")) {
return o.getDeployDate().getTime() >= dd.getTime();
}
return ddf.startsWith(">") && (o.getDeployDate().getTime() <= dd.getTime());
}
});
}
}
// Ordering
if (filter.getOrders() != null) {
ComparatorChain cChain = new ComparatorChain();
for (String key : filter.getOrders()) {
boolean ascending = true;
String orderKey = key;
if (key.startsWith("+") || key.startsWith("-")) {
orderKey = key.substring(1, key.length());
if (key.startsWith("-")) {
ascending = false;
}
}
Comparator c;
if ("name".equals(orderKey)) {
c = new Comparator<ProcessConf>() {
public int compare(ProcessConf o1, ProcessConf o2) {
return o1.getProcessId().getLocalPart().compareTo(o2.getProcessId().getLocalPart());
}
};
} else if ("namespace".equals(orderKey)) {
c = new Comparator<ProcessConf>() {
public int compare(ProcessConf o1, ProcessConf o2) {
String ns1 = o1.getProcessId().getNamespaceURI() == null ? "" : o1.getProcessId().getNamespaceURI();
String ns2 = o2.getProcessId().getNamespaceURI() == null ? "" : o2.getProcessId().getNamespaceURI();
return ns1.compareTo(ns2);
}
};
} else if ("version".equals(orderKey)) {
c = new Comparator<ProcessConf>() {
public int compare(ProcessConf o1, ProcessConf o2) {
return (int) (o1.getVersion() - o2.getVersion());
}
};
} else if ("deployed".equals(orderKey)) {
c = new Comparator<ProcessConf>() {
public int compare(ProcessConf o1, ProcessConf o2) {
return o1.getDeployDate().compareTo(o2.getDeployDate());
}
};
} else if ("status".equals(orderKey)) {
c = new Comparator<ProcessConf>() {
public int compare(ProcessConf o1, ProcessConf o2) {
return o1.getState().compareTo(o2.getState());
}
};
} else {
// unrecognized
if (log.isDebugEnabled()) {
log.debug("unrecognized order key" + orderKey);
}
continue;
}
cChain.addComparator(c, !ascending);
}
Collections.sort(confs, cChain);
}
}
return confs;
}
use of org.apache.commons.collections.comparators.ComparatorChain in project pmph by BCSquad.
the class TextbookServiceImpl method addOrUpdateTextBookList.
@SuppressWarnings("unchecked")
@Override
public List<Textbook> addOrUpdateTextBookList(Long materialId, Boolean isPublic, String textbooks, String sessionId) throws CheckedServiceException {
if (ObjectUtil.isNull(materialId)) {
throw new CheckedServiceException(CheckedExceptionBusiness.MATERIAL, CheckedExceptionResult.NULL_PARAM, "教材id不能为空");
}
if (ObjectUtil.isNull(isPublic)) {
throw new CheckedServiceException(CheckedExceptionBusiness.MATERIAL, CheckedExceptionResult.NULL_PARAM, "可见性区别不能为空");
}
if (StringUtil.isEmpty(textbooks)) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "书籍信息不能为空");
}
/*
* 检测同一教材下书名和版次都相同的数据
*/
List<Map<String, Object>> list = new ArrayList<>();
Gson gson = new GsonBuilder().serializeNulls().create();
List<Textbook> bookList = gson.fromJson(textbooks, new TypeToken<ArrayList<Textbook>>() {
}.getType());
/*
* 对数据进行排序
*/
ComparatorChain comparatorChain = new ComparatorChain();
comparatorChain.addComparator(new BeanComparator<Textbook>("sort"));
Collections.sort(bookList, comparatorChain);
/*
* 查询此教材下现有的书籍
*/
List<Textbook> textbookList = textbookDao.getTextbookByMaterialId(materialId);
List<Long> ids = new ArrayList<>();
for (Textbook textbook : textbookList) {
ids.add(textbook.getId());
}
// 装数据库中本来已经有的书籍id
List<Long> delBook = new ArrayList<>();
// 判断书序号的连续性计数器
int count = 1;
for (Textbook book : bookList) {
if (ObjectUtil.isNull(book.getMaterialId())) {
book.setMaterialId(materialId);
}
if (StringUtil.isEmpty(book.getTextbookName())) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "书籍名称不能为空");
}
if (StringUtil.strLength(book.getTextbookName()) > 50) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.ILLEGAL_PARAM, "书籍名称字数不能超过25个,请修改后再提交");
}
if (ObjectUtil.isNull(book.getTextbookRound())) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "书籍轮次不能为空");
}
if (book.getTextbookRound().intValue() > 2147483647) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.ILLEGAL_PARAM, "图书轮次过大,请修改后再提交");
}
if (ObjectUtil.isNull(book.getSort())) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "图书序号不能为空");
}
if (book.getSort().intValue() > 2147483647) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.ILLEGAL_PARAM, "图书序号过大,请修改后再提交");
}
if (ObjectUtil.isNull(book.getFounderId())) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "创建人id不能为空");
}
if (count != book.getSort()) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.ILLEGAL_PARAM, "书籍序号必须从1开始且连续");
}
Map<String, Object> map = new HashMap<>();
map.put(book.getTextbookName(), book.getTextbookRound());
if (list.contains(map)) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.ILLEGAL_PARAM, "同一教材下书名和版次不能都相同");
}
/*
* 设置书目录时保存操作传回来的对象里可能有已经存在的书籍,已经存在的修改 相关信息,没有的进行新增操作
*/
if (ObjectUtil.notNull(textbookDao.getTextbookById(book.getId()))) {
textbookDao.updateTextbook(book);
delBook.add(book.getId());
} else {
textbookDao.addTextbook(book);
}
list.add(map);
count++;
}
/* 设置书目录时若删除了部分书籍,找出这些书籍的id并将表中的相关数据删除掉 */
ids.removeAll(delBook);
if (CollectionUtil.isNotEmpty(ids)) {
for (Long id : ids) {
if (CollectionUtil.isNotEmpty(decPositionService.listDecPositionsByTextbookId(id)) && decPositionService.listDecPositionsByTextbookId(id).size() > 0) {
throw new CheckedServiceException(CheckedExceptionBusiness.TEXTBOOK, CheckedExceptionResult.NULL_PARAM, "被申报的书籍不允许被删除");
} else {
textbookDao.deleteTextbookById(id);
}
}
}
/* 修改对应的教材的可见性区别 */
Material material = new Material();
material.setId(materialId);
material.setIsPublic(isPublic);
materialService.updateMaterial(material, sessionId);
return textbookDao.getTextbookByMaterialId(materialId);
}
Aggregations