Search in sources :

Example 1 with ObjectStore

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) {
        }
    }
}
Also used : ProfileManager(org.intermine.api.profile.ProfileManager) ObjectStoreException(org.intermine.objectstore.ObjectStoreException) ObjectStore(org.intermine.objectstore.ObjectStore) FileReader(java.io.FileReader) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) ObjectStoreWriter(org.intermine.objectstore.ObjectStoreWriter) ObjectStoreException(org.intermine.objectstore.ObjectStoreException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Example 2 with ObjectStore

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);
        }
    }
}
Also used : ProfileManager(org.intermine.api.profile.ProfileManager) ObjectStore(org.intermine.objectstore.ObjectStore) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) FileWriter(java.io.FileWriter) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) ObjectStoreWriter(org.intermine.objectstore.ObjectStoreWriter) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Example 3 with ObjectStore

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);
        }
    }
}
Also used : ProfileManager(org.intermine.api.profile.ProfileManager) TagManagerFactory(org.intermine.api.profile.TagManagerFactory) ObjectStoreException(org.intermine.objectstore.ObjectStoreException) ObjectStore(org.intermine.objectstore.ObjectStore) HashMap(java.util.HashMap) Reader(java.io.Reader) FileReader(java.io.FileReader) ApiTemplate(org.intermine.api.template.ApiTemplate) Profile(org.intermine.api.profile.Profile) ObjectStoreException(org.intermine.objectstore.ObjectStoreException) BuildException(org.apache.tools.ant.BuildException) TemplateQuery(org.intermine.template.TemplateQuery) TagManager(org.intermine.api.profile.TagManager) FileReader(java.io.FileReader) Tag(org.intermine.api.userprofile.Tag) BuildException(org.apache.tools.ant.BuildException) ObjectStoreWriter(org.intermine.objectstore.ObjectStoreWriter) HashSet(java.util.HashSet)

Example 4 with ObjectStore

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;
}
Also used : InterMineObject(org.intermine.model.InterMineObject) ObjectStoreException(org.intermine.objectstore.ObjectStoreException) ObjectStore(org.intermine.objectstore.ObjectStore) InterMineAPI(org.intermine.api.InterMineAPI) Properties(java.util.Properties) InterMineLUIConverter(org.intermine.web.uri.InterMineLUIConverter) LinkedHashMap(java.util.LinkedHashMap) InterMineLUI(org.intermine.web.uri.InterMineLUI) JSONObject(org.json.JSONObject) InterMineObject(org.intermine.model.InterMineObject) PermanentURIHelper(org.intermine.web.logic.PermanentURIHelper)

Example 5 with ObjectStore

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());
    }
}
Also used : ObjectStore(org.intermine.objectstore.ObjectStore) ClassDescriptor(org.intermine.metadata.ClassDescriptor) FileWriter(java.io.FileWriter) IOException(java.io.IOException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) Model(org.intermine.metadata.Model) ReferenceDescriptor(org.intermine.metadata.ReferenceDescriptor) BuildException(org.apache.tools.ant.BuildException) CollectionDescriptor(org.intermine.metadata.CollectionDescriptor) File(java.io.File) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

Aggregations

ObjectStore (org.intermine.objectstore.ObjectStore)94 Query (org.intermine.objectstore.query.Query)37 InterMineObject (org.intermine.model.InterMineObject)34 QueryClass (org.intermine.objectstore.query.QueryClass)30 SingletonResults (org.intermine.objectstore.query.SingletonResults)28 InterMineAPI (org.intermine.api.InterMineAPI)25 Results (org.intermine.objectstore.query.Results)22 HashSet (java.util.HashSet)20 HttpSession (javax.servlet.http.HttpSession)19 BuildException (org.apache.tools.ant.BuildException)16 ObjectStoreException (org.intermine.objectstore.ObjectStoreException)16 ContainsConstraint (org.intermine.objectstore.query.ContainsConstraint)15 ObjectStoreInterMineImpl (org.intermine.objectstore.intermine.ObjectStoreInterMineImpl)13 ResultsRow (org.intermine.objectstore.query.ResultsRow)13 FileWriter (java.io.FileWriter)12 IOException (java.io.IOException)12 List (java.util.List)12 QueryObjectReference (org.intermine.objectstore.query.QueryObjectReference)12 ArrayList (java.util.ArrayList)11 Profile (org.intermine.api.profile.Profile)11