use of org.intermine.objectstore.ObjectStore in project intermine by intermine.
the class ProfileReadTask method execute.
/**
* Execute the task - read the profiles.
* @throws BuildException if there is a problem while reading from the file or writing to the
* profiles.
*/
public void execute() {
if (fileName == null) {
throw new BuildException("fileName parameter not set");
}
if (userProfileAlias == null) {
throw new BuildException("userProfileAlias parameter not set");
}
// Needed so that STAX can find it's implementation classes
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
FileReader reader = null;
try {
reader = new FileReader(fileName);
} catch (IOException e) {
throw new BuildException("failed to open input file: " + fileName, e);
}
ObjectStoreWriter osw = null;
try {
ObjectStore os = ObjectStoreFactory.getObjectStore(osAlias);
ObjectStoreWriter userProfileOS = ObjectStoreWriterFactory.getObjectStoreWriter(userProfileAlias);
ProfileManager pm = new ProfileManager(os, userProfileOS);
osw = os.getNewWriter();
ProfileManagerBinding.unmarshal(reader, pm, osw, false);
} catch (Exception e) {
throw new BuildException(e);
} finally {
Thread.currentThread().setContextClassLoader(cl);
try {
reader.close();
} catch (IOException e) {
throw new BuildException("failed to close input file: " + fileName, e);
}
try {
if (osw != null) {
osw.close();
}
} catch (ObjectStoreException e) {
}
}
}
use of org.intermine.objectstore.ObjectStore in project intermine by intermine.
the class ProfileWriteTask method execute.
/**
* Execute the task - write the profiles as XML.
* @throws BuildException if there is a problem while writing to the file or reading the
* profiles.
*/
public void execute() {
if (fileName == null) {
throw new BuildException("fileName parameter not set");
}
// Needed so that STAX can find it's implementation classes
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
FileWriter fw = null;
try {
fw = new FileWriter(fileName);
} catch (IOException e) {
throw new BuildException("failed to open output file: " + fileName, e);
}
try {
ObjectStore os = ObjectStoreFactory.getObjectStore(osAlias);
ObjectStoreWriter userProfileOS = ObjectStoreWriterFactory.getObjectStoreWriter(userProfileAlias);
ProfileManager pm = new ProfileManager(os, userProfileOS);
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(fw);
ProfileManagerBinding.marshal(pm, writer);
} catch (Exception e) {
throw new BuildException(e);
} finally {
Thread.currentThread().setContextClassLoader(cl);
try {
fw.close();
} catch (IOException e) {
throw new BuildException("failed to close output file: " + fileName, e);
}
}
}
use of org.intermine.objectstore.ObjectStore in project intermine by intermine.
the class LoadDefaultTemplatesTask method execute.
/**
* Load templates from an xml file into a userprofile account.
* {@inheritDoc}
*/
@SuppressWarnings("deprecation")
@Override
public void execute() {
log("Loading default templates and tags into profile " + username);
// Needed so that STAX can find its implementation classes
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
ObjectStoreWriter osw = null;
Profile profileDest = null;
try {
ObjectStore os = ObjectStoreFactory.getObjectStore(osAlias);
ObjectStoreWriter userProfileOS = ObjectStoreWriterFactory.getObjectStoreWriter(userProfileAlias);
ProfileManager pm = new ProfileManager(os, userProfileOS);
Reader reader = new FileReader(xmlFile);
// Copy into existing or new superuser profile
if (!pm.hasProfile(username)) {
String password = superuserPassword;
if ((password == null) || "".equals(password) || "${superuser.initialPassword}".equals(password)) {
password = generatePassword();
}
LOG.info("Creating profile for " + username);
profileDest = new Profile(pm, username, null, password, new HashMap<String, SavedQuery>(), new HashMap<String, InterMineBag>(), new HashMap<String, ApiTemplate>(), null, true, true);
profileDest.disableSaving();
pm.createProfile(profileDest);
} else {
LOG.info("Profile for " + username + ", clearing template queries");
profileDest = pm.getProfile(username, pm.getPassword(username));
Map<String, TemplateQuery> tmpls = new HashMap<String, TemplateQuery>(profileDest.getSavedTemplates());
for (String templateName : tmpls.keySet()) {
profileDest.deleteTemplate(templateName, null, true);
}
}
// Unmarshal
Set<Tag> tags = new HashSet<Tag>();
osw = os.getNewWriter();
Profile profileSrc = ProfileBinding.unmarshal(reader, pm, profileDest.getUsername(), profileDest.getPassword(), tags, osw, PathQuery.USERPROFILE_VERSION);
if (profileDest.getSavedTemplates().size() == 0) {
for (ApiTemplate template : profileSrc.getSavedTemplates().values()) {
String append = "";
if (!template.isValid()) {
append = " [invalid]";
}
log("Adding template \"" + template.getName() + "\"" + append);
profileDest.saveTemplate(template.getName(), template);
}
}
// Tags not loaded automatically when unmarshalling profile
TagManager tagManager = new TagManagerFactory(userProfileOS).getTagManager();
for (Tag tag : tags) {
if (tagManager.getTags(tag.getTagName(), tag.getObjectIdentifier(), tag.getType(), profileDest.getUsername()).isEmpty()) {
try {
tagManager.addTag(tag.getTagName(), tag.getObjectIdentifier(), tag.getType(), profileDest);
} catch (TagManager.TagException e) {
LOG.error("Error happened during adding tag. Ignored. Tag: " + tag.toString(), e);
} catch (RuntimeException ex) {
LOG.error("Error happened during adding tag. Ignored. Tag: " + tag.toString(), ex);
}
}
}
} catch (Exception e) {
LOG.error(e.getMessage());
throw new BuildException(e);
} finally {
if (profileDest != null) {
profileDest.enableSaving();
}
Thread.currentThread().setContextClassLoader(cl);
try {
if (osw != null) {
osw.close();
}
} catch (ObjectStoreException e) {
// not much we can do here
LOG.error("exception while closing object store writer", e);
}
}
}
use of org.intermine.objectstore.ObjectStore in project intermine by intermine.
the class SemanticMarkupFormatter method formatBioEntity.
/**
* Returns bioschema.org markups to be added to the report page of bio entities
* @param request the HttpServletRequest
* @param id intermine internal id
*
* @return the map containing the markups
*/
public static Map<String, Object> formatBioEntity(HttpServletRequest request, int id) {
if (!isEnabled()) {
return null;
}
InterMineAPI im = InterMineContext.getInterMineAPI();
ObjectStore os = im.getObjectStore();
InterMineObject entity = null;
String type = null;
try {
entity = os.getObjectById(id);
type = DynamicUtil.getSimpleClass(entity).getSimpleName();
if ("DataSet".equalsIgnoreCase(type)) {
return formatDataSet(request, entity);
}
} catch (ObjectStoreException ose) {
LOG.warn("Failed to find object with id: " + id, ose);
}
Map<String, Object> semanticMarkup = formatBioEntity(entity);
if (semanticMarkup == null) {
return null;
}
semanticMarkup.put("@context", BIO_SCHEMA);
Properties props = PropertiesUtil.getProperties();
semanticMarkup.put("version", props.getProperty("project.releaseVersion"));
Map<String, String> isPartOf = new LinkedHashMap<>();
isPartOf.put("@type", DATASET_TYPE);
isPartOf.put("name", props.getProperty("project.title"));
isPartOf.put("description", props.getProperty("project.subTitle"));
semanticMarkup.put("isPartOf", isPartOf);
InterMineLUI lui = (new InterMineLUIConverter()).getInterMineLUI(id);
if (lui != null) {
PermanentURIHelper helper = new PermanentURIHelper(request);
String permanentURL = helper.getPermanentURL(lui);
semanticMarkup.put("url", permanentURL);
semanticMarkup.put("@id", permanentURL);
}
return semanticMarkup;
}
use of org.intermine.objectstore.ObjectStore in project intermine by intermine.
the class GenerateUpdateTriggersTask method execute.
/**
* {@inheritDoc}
*/
@Override
public final void execute() {
if (destDir == null) {
throw new BuildException("destDir attribute is not set");
}
if (osname == null) {
throw new BuildException("osname attribute is not set");
}
try {
ObjectStore os;
try {
os = ObjectStoreFactory.getObjectStore(osname);
} catch (Exception e) {
throw new RuntimeException("Cannot connect to objectstore: " + e.getMessage());
}
Model model = os.getModel();
String adderFileName = "add-update-triggers.sql";
String removerFileName = "remove-update-triggers.sql";
String keyCheckFileName = "key-checker.sql";
FileWriter adderW;
FileWriter removerW;
FileWriter keyCheckW;
try {
adderW = new FileWriter(new File(destDir, adderFileName));
removerW = new FileWriter(new File(destDir, removerFileName));
keyCheckW = new FileWriter(new File(destDir, keyCheckFileName));
} catch (IOException e) {
throw new BuildException("Cannot open SQL file: " + e.getMessage());
}
PrintWriter adderPW = new PrintWriter(adderW);
PrintWriter removerPW = new PrintWriter(removerW);
PrintWriter keyCheckPW = new PrintWriter(keyCheckW);
// set the output to 'tuples_only'. We do not want the heading or row count.
// Should we try to capture setting?
keyCheckPW.println("\\pset tuples_only ON");
adderPW.print(getAddDisclaimer());
removerPW.print(getRemoveDisclaimer());
// keep track of indirections we generate SQL in the foreign key check
// to avoid redundancies
HashSet<CollectionDescriptor> indirections = new HashSet<CollectionDescriptor>();
adderPW.print(getAddSequence());
adderPW.print(getAddTruncateBlockFunction());
for (ClassDescriptor cld : model.getBottomUpLevelTraversal()) {
if (!"InterMineObject".equals(cld.getUnqualifiedName()) && cld.getFieldDescriptorByName("id") != null) {
adderPW.print(getAddDefaultClassConstraint(cld));
adderPW.print(getAddTruncateBlockTrigger(cld));
removerPW.print(getRemoveDefaultClassConstraint(cld));
removerPW.print(getRemoveTruncateBlockTrigger(cld));
Set<ClassDescriptor> sCDs = cld.getSuperDescriptors();
// add/remove actions for a superclass
for (ClassDescriptor superCld : sCDs) {
if (!"InterMineObject".equals(superCld.getUnqualifiedName())) {
adderPW.print(getAddSuperClassActions(cld, superCld));
removerPW.print(getRemoveSuperClassActions(cld, superCld));
}
}
// add/remove actions for InterMineObject table
adderPW.print(getAddIMOActions(cld));
removerPW.print(getRemoveIMOActions(cld));
// add/remove actions caused by a referenced record deletion
for (ReferenceDescriptor rD : cld.getAllReferenceDescriptors()) {
keyCheckPW.print(getForeignKeyCheck(cld, rD));
adderPW.print(getAddDeleteReferenceAction(cld, rD));
removerPW.print(getRemoveDeleteReferenceAction(cld, rD));
}
// add/remove actions caused by a collection record deletion
for (CollectionDescriptor cD : cld.getAllCollectionDescriptors()) {
if (!indirections.contains(cD) && cD.relationType() == FieldDescriptor.M_N_RELATION) {
indirections.add(cD);
keyCheckPW.print(getCollectionKeyCheck(cld, cD, model.getVersion()));
adderPW.print(getAddDeleteCollectionAction(cld, cD, model.getVersion()));
removerPW.print(getRemoveDeleteCollectionAction(cld, cD, model.getVersion()));
}
}
}
}
removerPW.print(getRemoveSequence());
removerPW.print(getRemoveTruncateBlockFunction());
adderPW.print(getAddDisclaimer());
adderPW.close();
removerPW.print(getRemoveDisclaimer());
removerPW.close();
// turn tuples_only off
keyCheckPW.println("\\pset tuples_only OFF");
keyCheckPW.close();
} catch (Exception e) {
throw new BuildException("Failed to build SQL triggers: " + e.getMessage());
}
}
Aggregations