use of org.bedework.calfacade.responses.Response in project bw-calendar-engine by Bedework.
the class DumpPrincipal method open.
public boolean open(final String dirName) {
if (!super.open()) {
return false;
}
/* Create a directory for the principal */
Response resp = makeDir(dirName, true);
if (resp.getStatus() == failed) {
return false;
}
if (resp.getStatus() == exists) {
// Duplicate user entry?
incCount(DumpGlobals.duplicateUsers);
for (int i = 0; i < 100; i++) {
resp = makeDir(dirName + "-dup-" + i, true);
if (resp.getStatus() == failed) {
return false;
}
if (resp.getStatus() == ok) {
break;
}
if (i == 99) {
addLn("Too many duplicates for " + dirName);
return false;
}
popPath();
}
}
icalTrans = new IcalTranslator(getSvc().getIcalCallback(true));
created = new TreeSet<>();
return true;
}
use of org.bedework.calfacade.responses.Response in project bw-calendar-engine by Bedework.
the class Dumper method makeDir.
/**
* Add a directory using stack top as path and pushes the new path
*
* @param name dirname
* @return Response with status ok if created
*/
protected Response makeDir(final String name, final boolean pushDup) {
final Response resp = new Response();
try {
final Path p = FileSystems.getDefault().getPath(topPath(), name);
final File f = p.toFile();
if (f.exists()) {
globals.info.addLn("Path " + p + " already exists.");
if (pushDup) {
pushPath(p.toString());
}
return Response.notOk(resp, Status.exists, "Path " + p + " already exists.");
}
if (!f.mkdirs()) {
throw new CalFacadeException("Unable to create directory " + p);
}
pushPath(p.toString());
return resp;
} catch (final Throwable t) {
return Response.error(resp, t);
}
}
use of org.bedework.calfacade.responses.Response in project bw-calendar-engine by Bedework.
the class BwIndexEsImpl method getSearchResult.
@Override
public List<SearchResultEntry> getSearchResult(final SearchResult sres, final int offset, final int num, final int desiredAccess) throws CalFacadeException {
if (debug) {
debug("offset: " + offset + ", num: " + num);
}
final EsSearchResult res = (EsSearchResult) sres;
res.pageStart = offset;
final SearchRequestBuilder srb = getClient().prepareSearch(searchIndexes);
if (res.curQuery != null) {
srb.setQuery(res.curQuery);
}
srb.setSearchType(SearchType.QUERY_THEN_FETCH).setPostFilter(res.curFilter).setFrom(res.pageStart);
final int size;
if (num < 0) {
size = (int) sres.getFound();
} else {
size = num;
}
// TODO - need a configurable absolute max size for fetches
srb.setSize(size);
final List<SearchResultEntry> entities = new ArrayList<>(size);
if (!Util.isEmpty(res.curSort)) {
SortOrder so;
for (final SortTerm st : res.curSort) {
if (st.isAscending()) {
so = SortOrder.ASC;
} else {
so = SortOrder.DESC;
}
srb.addSort(new FieldSortBuilder(ESQueryFilter.makePropertyRef(st.getProperties())).order(so));
}
}
if (res.requiresSecondaryFetch) {
// Limit to href then fetch those
srb.addField(ESQueryFilter.hrefJname);
}
final SearchResponse resp = srb.execute().actionGet();
if (resp.status() != RestStatus.OK) {
if (debug) {
debug("Search returned status " + resp.status());
}
}
final SearchHits hitsResp = resp.getHits();
if ((hitsResp.getHits() == null) || (hitsResp.getHits().length == 0)) {
return entities;
}
// Break condition: No hits are returned
if (hitsResp.hits().length == 0) {
return entities;
}
final List<SearchHit> hits;
if (res.requiresSecondaryFetch) {
hits = multiFetch(hitsResp, res.recurRetrieval);
if (hits == null) {
return entities;
}
} else {
hits = Arrays.asList(hitsResp.getHits());
}
final Map<String, Collection<BwEventAnnotation>> overrides = new HashMap<>();
final Collection<EventInfo> masters = new TreeSet<>();
EntityBuilder.checkFlushCache(currentChangeToken());
/* If we are retrieving events with a time range query and we are asking for the
* master + overrides then we need to check that the master really has an
* instance in the given time range */
final boolean checkTimeRange = (res.recurRetrieval.mode == Rmode.overrides) && ((res.latestStart != null) || (res.earliestEnd != null));
final Set<String> excluded = new TreeSet<>();
for (final SearchHit hit : hits) {
res.pageStart++;
final String dtype = hit.getType();
if (dtype == null) {
throw new CalFacadeException("org.bedework.index.noitemtype");
}
final String kval = hit.getId();
if (kval == null) {
throw new CalFacadeException("org.bedework.index.noitemkey");
}
final EntityBuilder eb = getEntityBuilder(hit.sourceAsMap());
Object entity = null;
switch(dtype) {
case docTypeCollection:
entity = eb.makeCollection();
break;
case docTypeCategory:
entity = eb.makeCat();
break;
case docTypeContact:
entity = eb.makeContact();
break;
case docTypeLocation:
entity = eb.makeLocation();
break;
case docTypeEvent:
case docTypePoll:
entity = eb.makeEvent(kval, res.recurRetrieval.mode == Rmode.expanded);
final EventInfo ei = (EventInfo) entity;
final BwEvent ev = ei.getEvent();
final Response evrestResp = new Response();
restoreEvProps(evrestResp, ei);
if (evrestResp.getStatus() != ok) {
warn("Failed restore of ev props: " + evrestResp);
}
final Acl.CurrentAccess ca = res.accessCheck.checkAccess(ev, desiredAccess, true);
if ((ca == null) || !ca.getAccessAllowed()) {
continue;
}
ei.setCurrentAccess(ca);
if (ev instanceof BwEventAnnotation) {
if (excluded.contains(ev.getUid())) {
continue;
}
// Treat as override
final Collection<BwEventAnnotation> ov = overrides.computeIfAbsent(ev.getHref(), k -> new TreeSet<>());
ov.add((BwEventAnnotation) ev);
continue;
}
if (checkTimeRange && dtype.equals(docTypeEvent) && ev.getRecurring()) {
if (Util.isEmpty(RecurUtil.getPeriods(ev, 99, 1, res.latestStart, res.earliestEnd).instances)) {
excluded.add(ev.getUid());
continue;
}
}
masters.add(ei);
break;
}
entities.add(new SearchResultEntry(entity, dtype, hit.getScore()));
}
for (final EventInfo ei : masters) {
final BwEvent ev = ei.getEvent();
if (ev.getRecurring()) {
final Collection<BwEventAnnotation> ov = overrides.get(ev.getHref());
if (ov != null) {
for (final BwEventAnnotation ann : ov) {
final BwEvent proxy = new BwEventProxy(ann);
ann.setTarget(ev);
ann.setMaster(ev);
final EventInfo oei = new EventInfo(proxy);
ei.addOverride(oei);
}
}
}
}
return entities;
}
Aggregations