use of org.openntf.nsfodp.commons.odp.notesapi.NNote in project org.openntf.nsfodp by OpenNTF.
the class DarwinoNDatabase method eachDesignNote.
@Override
public void eachDesignNote(BiConsumer<Integer, NNote> consumer) {
NSFSEARCHPROC proc = new NSFSEARCHPROC() {
@Override
public short callback(long searchMatchPtr, long summaryBufferPtr) throws DominoException {
SEARCH_MATCH searchMatch = new SEARCH_MATCH();
C.memcpy(searchMatch.getDataPtr(), 0, searchMatchPtr, 0, SEARCH_MATCH.sizeOf);
short noteClass = searchMatch.getNoteClass();
int noteId = searchMatch.getId().getNoteId();
byte retFlags = searchMatch.getSERetFlags();
boolean deleted = (noteClass & DominoAPI.NOTE_CLASS_NOTIFYDELETION) != 0;
// The use of "since" means that non-matching notes will be returned; check this flag to make sure
boolean isSearchMatch = (retFlags & DominoAPI.SE_FMATCH) != 0;
if (isSearchMatch && !deleted) {
try (NNote note = getNoteByID(noteId)) {
consumer.accept(noteId, note);
}
}
return DominoAPI.NOERROR;
}
};
try {
// $NON-NLS-1$
database.search("@All", proc, (short) 0, DominoAPI.NOTE_CLASS_ALLNONDATA, null, null);
} catch (DominoException e) {
throw new NDominoException(e.getStatus(), e);
} catch (FormulaException e) {
throw new NDominoException(0, e);
}
}
use of org.openntf.nsfodp.commons.odp.notesapi.NNote in project org.openntf.nsfodp by OpenNTF.
the class ODPCompiler method compileLotusScript.
private void compileLotusScript(NDatabase database, List<Integer> noteIds, boolean retry) {
if (!noteIds.isEmpty()) {
try {
// $NON-NLS-1$
Class.forName("lotus.domino.websvc.client.Stub");
} catch (ClassNotFoundException e) {
subTask(Messages.ODPCompiler_webServiceNotFound1);
subTask(Messages.ODPCompiler_webServiceNotFound2);
return;
}
subTask(Messages.ODPCompiler_compilingLotusScript);
// In lieu of a dependency graph, just keep bashing at the list until it's done
Queue<Integer> remaining = new ArrayDeque<>(noteIds);
Map<Integer, String> titles = new HashMap<>();
for (int i = 0; i < noteIds.size(); i++) {
Queue<Integer> nextPass = new ArrayDeque<>();
Integer noteId;
while ((noteId = remaining.poll()) != null) {
String title = null;
try (NNote note = database.getNoteByID(noteId)) {
// Check to see if this is the Shared Actions note, which we should skip to avoid trouble
if ((note.getNoteClassValue() & NsfNote.NOTE_CLASS_NONPRIV) == NsfNote.NOTE_CLASS_FORM) {
String flags = note.getAsString(StdNames.DESIGN_FLAGS, ' ');
if (NSFODPUtil.matchesFlagsPattern(flags, DFLAGPAT_SACTIONS_DESIGN)) {
continue;
}
}
// $NON-NLS-1$
title = note.get("$TITLE", String.class);
titles.put(noteId, title);
note.compileLotusScript();
note.sign();
note.save();
} catch (NLotusScriptCompilationException err) {
nextPass.add(noteId);
// $NON-NLS-1$
titles.put(noteId, title + " - " + err);
} catch (NDominoException err) {
if (err.getStatus() == 12051) {
// Same as above, but not encapsulated
// $NON-NLS-1$
titles.put(noteId, title + " - " + err);
nextPass.add(noteId);
} else if (err.getStatus() == 0x222) {
// Note item not found - occurs for non-LS elements
} else {
throw err;
}
}
}
remaining = nextPass;
if (nextPass.isEmpty()) {
break;
}
}
if (!retry && !remaining.isEmpty()) {
String notes = remaining.stream().map(// $NON-NLS-1$ //$NON-NLS-2$
noteId -> "Note ID " + noteId + ": " + titles.get(noteId)).collect(// $NON-NLS-1$
Collectors.joining("\n"));
throw new RuntimeException(MessageFormat.format(Messages.ODPCompiler_unableToCompileLotusScript, notes));
}
}
}
use of org.openntf.nsfodp.commons.odp.notesapi.NNote in project org.openntf.nsfodp by OpenNTF.
the class ODPExporter method export.
/**
* Exports the NSF to an on-disk project using the configured settings.
*
* @return a {@link Path} to the on-disk project root, either a directory or a ZIP file
* @throws IOException if there is a problem reading or writing filesystem data
*/
public Path export() throws IOException {
Path target;
Path returnPath;
ODPType odpType = this.odpType == null ? ODPType.DIRECTORY : this.odpType;
switch(odpType) {
case ZIP:
// $NON-NLS-1$ //$NON-NLS-2$
returnPath = Files.createTempFile(NSFODPUtil.getTempDirectory(), "org.openntf.nsfodp.exporter", ".zip");
// $NON-NLS-1$
target = NSFODPUtil.openZipPath(returnPath).getPath("/");
break;
case DIRECTORY:
default:
target = returnPath = Files.createTempDirectory(getClass().getName());
break;
}
try (NDXLExporter exporter = database.getAPI().createDXLExporter()) {
// Output database.properties in encapsulated format
// $NON-NLS-1$ //$NON-NLS-2$
Path databaseProperties = target.resolve("AppProperties").resolve("database.properties");
Files.createDirectories(databaseProperties.getParent());
Set<Integer> iconColl = new HashSet<>();
iconColl.add(NOTE_ID_SPECIAL | NOTE_CLASS_ICON);
try (NNote acl = database.getNoteByID(NOTE_ID_SPECIAL | NOTE_CLASS_ACL)) {
iconColl.add(acl.getNoteID());
}
try (OutputStream os = new CommonsSwiperOutputStream(databaseProperties, isSwiperFilter())) {
exporter.export(database, iconColl, os);
}
// Output the rest according to the settings
exporter.setForceNoteFormat(isBinaryDxl());
exporter.setRichTextAsItemData(isRichTextAsItemData());
database.eachDesignNote((noteId, note) -> {
NoteType type = null;
try {
type = NoteTypeUtil.forNote(note);
exportNote(note, exporter, target);
} catch (Throwable e) {
System.out.println(StringUtil.format(Messages.ODPExporter_nativeExceptionNoteId, Integer.toString(noteId, 16), e.getMessage(), type));
e.printStackTrace(System.out);
}
});
// Export several notes specially
int[] specialIds = new int[] { NOTE_CLASS_ICON, NOTE_CLASS_HELP, NOTE_CLASS_INFO };
for (int id : specialIds) {
try {
try (NNote iconNote = database.getNoteByID(NOTE_ID_SPECIAL | id)) {
if (iconNote != null && iconNote.isRefValid()) {
exportNote(iconNote, exporter, target);
}
}
} catch (NDominoException e) {
switch(e.getStatus()) {
case 578:
// "Special database object cannot be located", which is fine
break;
default:
e.printStackTrace();
System.out.println(StringUtil.format(Messages.ODPExporter_nativeExceptionSpecialNote, id, e.getMessage()));
break;
}
} catch (Throwable e) {
e.printStackTrace();
System.out.println(StringUtil.format(Messages.ODPExporter_nativeExceptionSpecialNote, id, e.getMessage()));
}
}
generateManifestMf(target);
generateEclipseProjectFile(target);
createClasspathDirectories(target);
createStubFiles(target);
}
if (odpType == ODPType.ZIP) {
target.getFileSystem().close();
}
return returnPath;
}
use of org.openntf.nsfodp.commons.odp.notesapi.NNote in project org.openntf.nsfodp by OpenNTF.
the class ODPCompiler method compile.
/**
* Runs the compilation process:
*
* <ol>
* <li>Installs all bundles from the provided update sites</li>
* <li>Initializes plugin contributions from installed bundles</li>
* <li>Compiles all XPage artifacts</li>
* <li>Constructs the NSF from the on-disk project</li>
* <li>Uninstalls any installed bundles</li>
* </ol>
*
* @param cl the base {@link ClassLoader} to use during compilation
* @return a {@link Path} representing the created database
* @throws Exception if there is a problem compiling any component
* @since 1.0.0
*/
public synchronized Path compile(ClassLoader cl) throws Exception {
Collection<Bundle> bundles = installBundles();
JavaSourceClassLoader classLoader = null;
Set<Path> cleanup = new HashSet<>();
try {
boolean hasXPages = odp.hasXPagesElements();
if (hasXPages) {
initRegistry();
Collection<String> dependencies = buildDependenciesCollection(cleanup);
dependencies.addAll(ODPUtil.expandRequiredBundles(bundleContext, odp.getRequiredBundles()));
// Add any Jars from the ODP
for (Path jar : odp.getJars()) {
// If the path is inside a JAR, extract it
if ("jar".equals(jar.toUri().getScheme())) {
// $NON-NLS-1$
// $NON-NLS-1$
Path tempJar = Files.createTempFile(NSFODPUtil.getTempDirectory(), jar.getFileName().toString(), ".jar");
cleanup.add(tempJar);
Files.copy(jar, tempJar, StandardCopyOption.REPLACE_EXISTING);
// $NON-NLS-1$
dependencies.add("jar:" + tempJar.toUri());
} else {
// $NON-NLS-1$
dependencies.add("jar:" + jar.toUri());
}
}
String[] classPath = dependencies.toArray(new String[dependencies.size()]);
List<String> options = Stream.concat(compilerOptions.stream(), // $NON-NLS-1$ //$NON-NLS-2$
Stream.of("-source", compilerLevel, "-target", compilerLevel)).collect(Collectors.toList());
classLoader = new JavaSourceClassLoader(cl, options, classPath);
// Bar loading of different-version SSJS classes from ndext
// $NON-NLS-1$
classLoader.getJavaFileManager().setNonDelegatingPackages(Arrays.asList("com.ibm.jscript"));
// Compile Java classes
compileJavaSources(classLoader);
compileCustomControls(classLoader);
compileXPages(classLoader);
}
try (NotesAPI session = NotesAPI.get()) {
Path file = createDatabase(session);
try (NDatabase database = session.openDatabase("", file.toAbsolutePath().toString())) {
// $NON-NLS-1$
try (NDXLImporter importer = session.createDXLImporter()) {
importDbProperties(importer, database);
importEarlyBasicElements(importer, database);
importLotusScriptLibraries(importer, database);
importBasicElements(importer, database);
importFileResources(importer, database);
importDbScript(importer, database);
if (hasXPages) {
Set<String> compiledClassNames = new HashSet<>(classLoader.getCompiledClassNames());
importCustomControls(importer, database, classLoader, compiledClassNames);
importXPages(importer, database, classLoader, compiledClassNames);
importJavaElements(importer, database, classLoader, compiledClassNames);
}
// Append a timestamp if requested
if (this.isAppendTimestampToTitle()) {
// $NON-NLS-1$
database.setTitle(database.getTitle() + " - " + TIMESTAMP.get().format(new Date()));
}
// Set the template info if requested
String templateName = this.getTemplateName();
if (StringUtil.isNotEmpty(templateName)) {
// $NON-NLS-1$
int noteId = database.getSharedFieldNoteID("$TemplateBuild");
NNote doc;
if (noteId != 0) {
doc = database.getNoteByID(noteId);
} else {
// Import an empty one
try (InputStream is = ODPCompiler.class.getResourceAsStream("/dxl/TemplateBuild.xml")) {
// $NON-NLS-1$
// $NON-NLS-1$
String dxl = StreamUtil.readString(is, "UTF-8");
// $NON-NLS-1$
List<Integer> ids = importDxl(importer, dxl, database, "$TemplateBuild blank field");
doc = database.getNoteByID(ids.get(0));
}
}
String version = this.getTemplateVersion();
if (StringUtil.isNotEmpty(version)) {
// $NON-NLS-1$
doc.set("$TemplateBuild", version);
}
// $NON-NLS-1$
doc.set("$TemplateBuildName", templateName);
// $NON-NLS-1$
doc.set("$TemplateBuildDate", new Date());
doc.save();
}
}
}
return file;
}
} catch (JavaCompilerException e) {
StringWriter o = new StringWriter();
PrintWriter errOut = new PrintWriter(o);
e.printExtraInformation(errOut);
throw new RuntimeException(MessageFormat.format(Messages.ODPCompiler_javaCompilationFailed, o), e);
} finally {
uninstallBundles(bundles);
if (classLoader != null) {
classLoader.close();
}
NSFODPUtil.deltree(cleanup);
}
}
use of org.openntf.nsfodp.commons.odp.notesapi.NNote in project org.openntf.nsfodp by OpenNTF.
the class ODPCompiler method importDxl.
/**
* @param importer the importer to use during the process
* @param dxl an XML {@link InputStream} to import
* @param database the database to import to
* @param name a human-readable name of the element, for logging
* @return a {@link List} of imported note IDs
* @since 3.4.0
*/
private List<Integer> importDxl(NDXLImporter importer, InputStream dxl, NDatabase database, String name) throws Exception {
try {
Collection<Integer> imported = new HashSet<>();
imported.addAll(importer.importDxl(database, dxl));
String logXml = importer.getResultLogXML();
if (StringUtil.isNotEmpty(logXml)) {
DxlImporterLog log = DxlImporterLog.forXml(logXml);
if (log.getErrors() != null && !log.getErrors().isEmpty()) {
String msg = log.getErrors().stream().map(e -> StringUtil.format("{2} (line={0}, column={1})", e.getLine(), e.getColumn(), e.getText())).collect(// $NON-NLS-1$
Collectors.joining(", "));
throw new Exception(MessageFormat.format("Exception importing {0}: {1}", name, msg));
} else if (log.getFatalErrors() != null && !log.getFatalErrors().isEmpty()) {
String msg = log.getFatalErrors().stream().map(DXLFatalError::getText).collect(// $NON-NLS-1$
Collectors.joining(", "));
throw new Exception(MessageFormat.format("Exception importing {0}: {1}", name, msg));
}
}
List<Integer> importedIds = new ArrayList<>();
for (Integer noteId : imported) {
importedIds.add(noteId);
try (NNote note = database.getNoteByID(noteId)) {
note.sign();
note.save();
}
}
return importedIds;
} catch (Exception ne) {
if (ne.getMessage().contains("DXL importer operation failed")) {
// $NON-NLS-1$
throw new RuntimeException(MessageFormat.format(Messages.ODPCompiler_dxlImportFailed, name, importer.getResultLogXML()), ne);
}
throw ne;
}
}
Aggregations