use of org.exist.xquery.value.DateTimeValue in project exist by eXist-db.
the class HistoryTrigger method makeCopy.
private void makeCopy(final DBBroker broker, final Txn txn, final DocumentImpl doc) throws TriggerException {
if (doc == null) {
return;
}
// construct the destination path
final XmldbURI path = rootPath.append(doc.getURI());
try {
// construct the destination document name
String dtValue = new DateTimeValue(new Date(doc.getLastModified())).getStringValue();
// multiple ':' are not allowed in URI so use '-'
dtValue = dtValue.replaceAll(":", "-");
// as we are using '-' instead of ':' do the same for '.'
dtValue = dtValue.replaceAll("\\.", "-");
final XmldbURI name = XmldbURI.create(dtValue);
// create the destination document
// TODO : how is the transaction handled ? It holds the locks !
final Collection destination = broker.getOrCreateCollection(txn, path);
broker.saveCollection(txn, destination);
broker.copyResource(txn, doc, destination, name);
} catch (final XPathException | IOException | PermissionDeniedException | LockException | EXistException xpe) {
throw new TriggerException(xpe);
}
}
use of org.exist.xquery.value.DateTimeValue in project exist by eXist-db.
the class FunCurrentDateTime method eval.
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
Sequence result = new DateTimeValue(context.getCalendar());
if (isCalledAs("current-dateTime")) {
// do nothing, result already in right form
} else if (isCalledAs("current-date")) {
result = result.convertTo(Type.DATE);
} else if (isCalledAs("current-time")) {
result = result.convertTo(Type.TIME);
} else {
throw new Error("Can't handle function " + getName().getLocalPart());
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.DateTimeValue in project exist by eXist-db.
the class PersistentLogin method register.
/**
* Register the user and generate a first login token which will be valid for the next
* call to {@link #lookup(String)}.
*
* The generated token will have the format base64(series-hash):base64(token-hash).
*
* @param user the user name
* @param password the password
* @param timeToLive timeout of the token
* @return a first login token
* @throws XPathException if a query error occurs
*/
public LoginDetails register(String user, String password, DurationValue timeToLive) throws XPathException {
DateTimeValue now = new DateTimeValue(new Date());
DateTimeValue expires = (DateTimeValue) now.plus(timeToLive);
LoginDetails login = new LoginDetails(user, password, timeToLive, expires.getTimeInMillis());
seriesMap.put(login.getSeries(), login);
return login;
}
use of org.exist.xquery.value.DateTimeValue in project exist by eXist-db.
the class DirectoryList method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (!context.getSubject().hasDbaRole()) {
XPathException xPathException = new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to call this function.");
logger.error("Invalid user", xPathException);
throw xPathException;
}
final String inputPath = args[0].getStringValue();
final Path baseDir = FileModuleHelper.getFile(inputPath);
final Sequence patterns = args[1];
if (logger.isDebugEnabled()) {
logger.debug("Listing matching files in directory: {}", baseDir);
}
context.pushDocumentContext();
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(new QName("list", NAMESPACE_URI, PREFIX), null);
builder.addAttribute(new QName("directory", null, null), baseDir.toString());
try {
final int patternsLen = patterns.getItemCount();
final String[] includes = new String[patternsLen];
for (int i = 0; i < patternsLen; i++) {
includes[i] = patterns.itemAt(0).getStringValue();
}
final DirectoryScanner directoryScanner = new DirectoryScanner();
directoryScanner.setIncludes(includes);
directoryScanner.setBasedir(baseDir.toFile());
directoryScanner.setCaseSensitive(true);
directoryScanner.scan();
for (final String includedFile : directoryScanner.getIncludedFiles()) {
final Path file = baseDir.resolve(includedFile);
if (logger.isDebugEnabled()) {
logger.debug("Found: {}", file.toAbsolutePath());
}
String relPath = file.toString().substring(baseDir.toString().length() + 1);
int lastSeparatorPosition = relPath.lastIndexOf(java.io.File.separatorChar);
String relDir = null;
if (lastSeparatorPosition >= 0) {
relDir = relPath.substring(0, lastSeparatorPosition);
relDir = relDir.replace(java.io.File.separatorChar, '/');
}
builder.startElement(new QName("file", NAMESPACE_URI, PREFIX), null);
builder.addAttribute(new QName("name", null, null), FileUtils.fileName(file));
Long sizeLong = FileUtils.sizeQuietly(file);
String sizeString = Long.toString(sizeLong);
String humanSize = getHumanSize(sizeLong, sizeString);
builder.addAttribute(new QName("size", null, null), sizeString);
builder.addAttribute(new QName("human-size", null, null), humanSize);
builder.addAttribute(new QName("modified", null, null), new DateTimeValue(new Date(Files.getLastModifiedTime(file).toMillis())).getStringValue());
if (relDir != null && !relDir.isEmpty()) {
builder.addAttribute(new QName("subdir", null, null), relDir);
}
builder.endElement();
}
builder.endElement();
return (NodeValue) builder.getDocument().getDocumentElement();
} catch (final IOException e) {
throw new XPathException(this, e.getMessage());
} finally {
context.popDocumentContext();
}
}
use of org.exist.xquery.value.DateTimeValue in project exist by eXist-db.
the class AbstractBackupDescriptor method getDate.
public Date getDate() {
if (date == null) {
try {
final Properties properties = getProperties();
final String dateStr = properties.getProperty("date");
if (dateStr != null) {
final DateTimeValue dtv = new DateTimeValue(dateStr);
date = dtv.getDate();
}
} catch (final IOException | XPathException e) {
}
if (date == null) {
// catch unexpected issues by setting the backup time as early as possible
date = new Date(0);
}
}
return (date);
}
Aggregations