use of org.alfresco.repo.dictionary.M2Model in project SearchServices by Alfresco.
the class ModelTracker method loadPersistedModels.
/**
*/
private void loadPersistedModels() {
HashMap<String, M2Model> modelMap = new HashMap<String, M2Model>();
if (alfrescoModelDir.exists() && alfrescoModelDir.isDirectory()) {
// A filter for XML files
FileFilter filter = new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.isFile() && pathname.getName().endsWith(".xml");
}
};
// List XML files
for (File file : alfrescoModelDir.listFiles(filter)) {
InputStream modelStream = null;
M2Model model;
try {
modelStream = new FileInputStream(file);
model = M2Model.createModel(modelStream);
} catch (IOException e) {
throw new AlfrescoRuntimeException("File not found: " + file, e);
} finally {
if (modelStream != null) {
try {
modelStream.close();
} catch (Exception e) {
}
}
}
// Model successfully loaded
for (M2Namespace namespace : model.getNamespaces()) {
modelMap.put(namespace.getUri(), model);
}
}
}
// Load the models ensuring that they are loaded in the correct order
HashSet<String> loadedModels = new HashSet<String>();
for (M2Model model : modelMap.values()) {
loadModel(modelMap, loadedModels, model);
}
if (modelMap.size() > 0) {
AlfrescoSolrDataModel.getInstance().afterInitModels();
}
}
use of org.alfresco.repo.dictionary.M2Model in project SearchServices by Alfresco.
the class ModelTracker method trackModelsImpl.
/**
* Tracks models. Reflects changes and updates on disk copy
*
* @throws AuthenticationException
* @throws IOException
* @throws JSONException
*/
private void trackModelsImpl() throws AuthenticationException, IOException, JSONException {
long start = System.nanoTime();
List<AlfrescoModelDiff> modelDiffs = client.getModelsDiff(coreName, this.infoSrv.getAlfrescoModels());
HashMap<String, M2Model> modelMap = new HashMap<String, M2Model>();
for (AlfrescoModelDiff modelDiff : modelDiffs) {
switch(modelDiff.getType()) {
case CHANGED:
AlfrescoModel changedModel = client.getModel(coreName, modelDiff.getModelName());
for (M2Namespace namespace : changedModel.getModel().getNamespaces()) {
modelMap.put(namespace.getUri(), changedModel.getModel());
}
break;
case NEW:
AlfrescoModel newModel = client.getModel(coreName, modelDiff.getModelName());
for (M2Namespace namespace : newModel.getModel().getNamespaces()) {
modelMap.put(namespace.getUri(), newModel.getModel());
}
break;
case REMOVED:
// We need to know the prefix for the uri to delete them
break;
}
}
HashSet<String> loadedModels = new HashSet<String>();
for (M2Model model : modelMap.values()) {
loadModel(modelMap, loadedModels, model);
}
if (loadedModels.size() > 0) {
this.infoSrv.afterInitModels();
}
for (AlfrescoModelDiff modelDiff : modelDiffs) {
switch(modelDiff.getType()) {
case CHANGED:
removeMatchingModels(alfrescoModelDir, modelDiff.getModelName());
M2Model changedModel = this.infoSrv.getM2Model(modelDiff.getModelName());
File changedFile = new File(alfrescoModelDir, getModelFileName(changedModel));
FileOutputStream cos = new FileOutputStream(changedFile);
changedModel.toXML(cos);
cos.flush();
cos.close();
break;
case NEW:
M2Model newModel = this.infoSrv.getM2Model(modelDiff.getModelName());
// add on file
File newFile = new File(alfrescoModelDir, getModelFileName(newModel));
FileOutputStream nos = new FileOutputStream(newFile);
newModel.toXML(nos);
nos.flush();
nos.close();
break;
case REMOVED:
// This will remove the model from the dictionary on completion
try {
removeMatchingModels(alfrescoModelDir, modelDiff.getModelName());
} finally {
AlfrescoSolrDataModel.getInstance().removeModel(modelDiff.getModelName());
}
break;
}
}
long end = System.nanoTime();
trackerStats.addModelTime(end - start);
if (true == runPostModelLoadInit) {
for (Object key : props.keySet()) {
String stringKey = (String) key;
if (stringKey.startsWith("alfresco.index.store")) {
StoreRef store = new StoreRef(props.getProperty(stringKey));
indexedStores.add(store);
}
if (stringKey.startsWith("alfresco.ignore.store")) {
StoreRef store = new StoreRef(props.getProperty(stringKey));
ignoredStores.add(store);
}
if (stringKey.startsWith("alfresco.index.tenant")) {
indexedTenants.add(props.getProperty(stringKey));
}
if (stringKey.startsWith("alfresco.ignore.tenant")) {
ignoredTenants.add(props.getProperty(stringKey));
}
if (stringKey.startsWith("alfresco.index.datatype")) {
QName qname = expandQName(props.getProperty(stringKey));
indexedDataTypes.add(qname);
}
if (stringKey.startsWith("alfresco.ignore.datatype")) {
QName qname = expandQName(props.getProperty(stringKey));
ignoredDataTypes.add(qname);
}
if (stringKey.startsWith("alfresco.index.type")) {
QName qname = expandQName(props.getProperty(stringKey));
indexedTypes.add(qname);
}
if (stringKey.startsWith("alfresco.ignore.type")) {
QName qname = expandQName(props.getProperty(stringKey));
ignoredTypes.add(qname);
}
if (stringKey.startsWith("alfresco.index.aspect")) {
QName qname = expandQName(props.getProperty(stringKey));
indexedAspects.add(qname);
}
if (stringKey.startsWith("alfresco.ignore.aspect")) {
QName qname = expandQName(props.getProperty(stringKey));
ignoredAspects.add(qname);
}
if (stringKey.startsWith("alfresco.index.field")) {
String name = expandName(props.getProperty(stringKey));
indexedFields.add(name);
}
if (stringKey.startsWith("alfresco.ignore.field")) {
String name = expandName(props.getProperty(stringKey));
ignoredFields.add(name);
}
}
runPostModelLoadInit = false;
}
}
use of org.alfresco.repo.dictionary.M2Model in project alfresco-remote-api by Alfresco.
the class CustomModelUploadPost method processUpload.
protected ImportResult processUpload(ZipFile zipFile, String filename) throws IOException {
if (zipFile.size() > 2) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_invalid_zip_package");
}
CustomModel customModel = null;
String shareExtModule = null;
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
final String entryName = entry.getName();
try (InputStream input = new BufferedInputStream(zipFile.getInputStream(entry), BUFFER_SIZE)) {
if (!(entryName.endsWith(CustomModelServiceImpl.SHARE_EXT_MODULE_SUFFIX)) && customModel == null) {
try {
M2Model m2Model = M2Model.createModel(input);
customModel = importModel(m2Model);
} catch (DictionaryException ex) {
if (shareExtModule == null) {
// Get the input stream again, as the zip file doesn't support reset.
try (InputStream moduleInputStream = new BufferedInputStream(zipFile.getInputStream(entry), BUFFER_SIZE)) {
shareExtModule = getExtensionModule(moduleInputStream, entryName);
}
if (shareExtModule == null) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_invalid_zip_entry_format", new Object[] { entryName });
}
} else {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_invalid_model_entry", new Object[] { entryName });
}
}
} else {
shareExtModule = getExtensionModule(input, entryName);
if (shareExtModule == null) {
throw new WebScriptException(Status.STATUS_BAD_REQUEST, "cmm.rest_api.model.import_invalid_ext_module_entry", new Object[] { entryName });
}
}
}
}
}
return new ImportResult(customModel, shareExtModule);
}
use of org.alfresco.repo.dictionary.M2Model in project alfresco-remote-api by Alfresco.
the class CustomModelImportTest method testUploadModel_UnsupportedModelElements.
public void testUploadModel_UnsupportedModelElements() throws Exception {
// Note: here we only test a couple of not-supported model elements to check for the correct status code.
// This test should be removed when we implement the required support
long timestamp = System.currentTimeMillis();
final String modelName = getClass().getSimpleName() + timestamp;
final String prefix = "prefix" + timestamp;
final String uri = "uriNamespace" + timestamp;
final String aspectName = prefix + QName.NAMESPACE_PREFIX + "testAspec";
final String typeName = prefix + QName.NAMESPACE_PREFIX + "testType";
final String associationName = prefix + QName.NAMESPACE_PREFIX + "testAssociation";
M2Model model = M2Model.createModel(prefix + QName.NAMESPACE_PREFIX + modelName);
model.createNamespace(uri, prefix);
model.setAuthor("John Doe");
model.createAspect(aspectName);
model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);
M2Type type = model.createType(typeName);
// Add 'association' not supported yet.
M2Association association = type.createAssociation(associationName);
association.setSourceMandatory(false);
association.setSourceMany(false);
association.setTargetMandatory(false);
association.setTargetClassName("cm:content");
ByteArrayOutputStream xml = new ByteArrayOutputStream();
model.toXML(xml);
ZipEntryContext context = new ZipEntryContext(modelName + ".xml", xml.toByteArray());
File zipFile = createZip(context);
PostRequest postRequest = buildMultipartPostRequest(zipFile);
// <associations> element is not supported yet
sendRequest(postRequest, 409);
type.removeAssociation(associationName);
// Add 'mandatory-aspect' not supported yet.
type.addMandatoryAspect(aspectName);
xml = new ByteArrayOutputStream();
model.toXML(xml);
context = new ZipEntryContext(modelName + ".xml", xml.toByteArray());
zipFile = createZip(context);
postRequest = buildMultipartPostRequest(zipFile);
// <mandatory-aspects> element is not supported yet
sendRequest(postRequest, 409);
}
use of org.alfresco.repo.dictionary.M2Model in project alfresco-remote-api by Alfresco.
the class CustomModelImportTest method testInvalidNumberOfZipEntries.
public void testInvalidNumberOfZipEntries() throws Exception {
long timestamp = System.currentTimeMillis();
String modelName = getClass().getSimpleName() + timestamp;
String prefix = "prefix" + timestamp;
String uri = "uriNamespace" + timestamp;
// Model one
M2Model modelOne = M2Model.createModel(prefix + QName.NAMESPACE_PREFIX + modelName);
modelOne.createNamespace(uri, prefix);
modelOne.setDescription("Model 1");
ByteArrayOutputStream xml = new ByteArrayOutputStream();
modelOne.toXML(xml);
ZipEntryContext contextOne = new ZipEntryContext(modelName + ".xml", xml.toByteArray());
// Model two
modelName += "two";
prefix += "two";
uri += "two";
M2Model modelTwo = M2Model.createModel(prefix + QName.NAMESPACE_PREFIX + modelName);
modelTwo.createNamespace(uri, prefix);
modelTwo.setDescription("Model 2");
xml = new ByteArrayOutputStream();
modelTwo.toXML(xml);
ZipEntryContext contextTwo = new ZipEntryContext(modelName + ".xml", xml.toByteArray());
// Model three
modelName += "three";
prefix += "three";
uri += "three";
M2Model modelThree = M2Model.createModel(prefix + QName.NAMESPACE_PREFIX + modelName);
modelThree.createNamespace(uri, prefix);
modelThree.setDescription("Model 3");
xml = new ByteArrayOutputStream();
modelThree.toXML(xml);
ZipEntryContext contextThree = new ZipEntryContext(modelName + ".xml", xml.toByteArray());
File zipFile = createZip(contextOne, contextTwo, contextThree);
PostRequest postRequest = buildMultipartPostRequest(zipFile);
// more than two zip entries
sendRequest(postRequest, 400);
}
Aggregations