Search in sources :

Example 1 with Function

use of org.opencastproject.util.data.Function in project opencast by opencast.

the class BundleInfoJpa method findAll.

/**
 * Find all bundles whose symbolic names start with one of the given prefixes.
 */
public static Function<EntityManager, List<BundleInfoJpa>> findAll(final String... prefixes) {
    return new Function<EntityManager, List<BundleInfoJpa>>() {

        @Override
        public List<BundleInfoJpa> apply(EntityManager em) {
            final CriteriaBuilder cb = em.getCriteriaBuilder();
            final CriteriaQuery<BundleInfoJpa> q = cb.createQuery(BundleInfoJpa.class);
            final Root<BundleInfoJpa> r = q.from(BundleInfoJpa.class);
            q.select(r);
            final Expression<String> symbolicNamePath = r.get("bundleSymbolicName");
            final Predicate[] likes = toArray(Predicate.class, mlist(prefixes).map(new Function<String, Predicate>() {

                @Override
                public Predicate apply(String prefix) {
                    return cb.like(symbolicNamePath, prefix + "%");
                }
            }).value());
            q.where(cb.or(likes));
            q.orderBy(cb.asc(r.get("host")), cb.asc(symbolicNamePath));
            return em.createQuery(q).getResultList();
        }
    };
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Function(org.opencastproject.util.data.Function) EntityManager(javax.persistence.EntityManager) Predicate(javax.persistence.criteria.Predicate)

Example 2 with Function

use of org.opencastproject.util.data.Function in project opencast by opencast.

the class XmlGen method $eTxtBlank.

protected Node $eTxtBlank(final String name, String text) {
    return $txtBlank(text).map(new Function<Node, Node>() {

        @Override
        public Node apply(Node text) {
            final Element e = createElemDefaultNs(name);
            e.appendChild(text);
            return e;
        }
    }).getOrElse(nodeZero);
}
Also used : Function(org.opencastproject.util.data.Function) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element)

Example 3 with Function

use of org.opencastproject.util.data.Function in project opencast by opencast.

the class OaiXmlGen method resumptionToken.

/**
 * Create the resumption token and store the query.
 */
Node resumptionToken(final Option<String> resumptionToken, final String metadataPrefix, final SearchResult result, Date until, Option<String> set) {
    // compute the token value...
    final Option<Option<String>> token;
    if (result.size() == result.getLimit()) {
        SearchResultItem lastResult = result.getItems().get((int) (result.size() - 1));
        // more to come...
        token = some(some(repository.saveQuery(new ResumableQuery(metadataPrefix, lastResult.getModificationDate(), until, set))));
    } else if (resumptionToken.isSome()) {
        // last page reached
        token = some(Option.<String>none());
    } else {
        token = none();
    }
    // ... then transform it into a node
    return token.map(new Function<Option<String>, Node>() {

        @Override
        public Node apply(Option<String> token) {
            return $e("resumptionToken", // $a("cursor", Integer.toString(offset)),
            token.map(mkText).getOrElse(nodeZero));
        }
    }).getOrElse(nodeZero);
}
Also used : Function(org.opencastproject.util.data.Function) SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) Option(org.opencastproject.util.data.Option)

Example 4 with Function

use of org.opencastproject.util.data.Function in project opencast by opencast.

the class PersistenceEnvs method persistenceEnvironment.

/**
 * Create a new, concurrently usable persistence environment which uses JPA local transactions.
 * <p>
 * Transaction propagation is supported on a per thread basis.
 */
public static PersistenceEnv persistenceEnvironment(final EntityManagerFactory emf) {
    final Transactional startTx = new Transactional() {

        @Override
        public <A> A tx(Function<EntityManager, A> transactional) {
            for (final EntityManager em : createEntityManager(emf)) {
                final EntityTransaction tx = em.getTransaction();
                try {
                    tx.begin();
                    emStore.set(Option.<Transactional>some(new Transactional() {

                        @Override
                        public <A> A tx(Function<EntityManager, A> transactional) {
                            return transactional.apply(em);
                        }
                    }));
                    A ret = transactional.apply(em);
                    tx.commit();
                    return ret;
                } catch (Exception e) {
                    if (tx.isActive()) {
                        tx.rollback();
                    }
                    // propagate exception
                    return chuck(e);
                } finally {
                    if (em.isOpen())
                        em.close();
                    emStore.remove();
                }
            }
            return chuck(new IllegalStateException("EntityManager is already closed"));
        }
    };
    return new PersistenceEnv() {

        Transactional currentTx() {
            return emStore.get().getOrElse(startTx);
        }

        @Override
        public <A> A tx(Function<EntityManager, A> transactional) {
            return currentTx().tx(transactional);
        }

        @Override
        public void close() {
            emf.close();
        }
    };
}
Also used : Function(org.opencastproject.util.data.Function) EntityTransaction(javax.persistence.EntityTransaction) PersistenceUtil.createEntityManager(org.opencastproject.util.persistence.PersistenceUtil.createEntityManager) EntityManager(javax.persistence.EntityManager)

Example 5 with Function

use of org.opencastproject.util.data.Function in project opencast by opencast.

the class DownloadDistributionServiceImplTest method setUp.

@Before
public void setUp() throws Exception {
    final File mediaPackageRoot = new File(getClass().getResource("/mediapackage.xml").toURI()).getParentFile();
    mp = MediaPackageParser.getFromXml(IOUtils.toString(getClass().getResourceAsStream("/mediapackage.xml"), "UTF-8"));
    distributionRoot = new File(mediaPackageRoot, "static");
    service = new DownloadDistributionServiceImpl();
    StatusLine statusLine = EasyMock.createNiceMock(StatusLine.class);
    EasyMock.expect(statusLine.getStatusCode()).andReturn(HttpServletResponse.SC_OK).anyTimes();
    EasyMock.replay(statusLine);
    HttpResponse response = EasyMock.createNiceMock(HttpResponse.class);
    EasyMock.expect(response.getStatusLine()).andReturn(statusLine).anyTimes();
    EasyMock.replay(response);
    final TrustedHttpClient httpClient = EasyMock.createNiceMock(TrustedHttpClient.class);
    EasyMock.expect(httpClient.execute((HttpUriRequest) EasyMock.anyObject())).andReturn(response).anyTimes();
    EasyMock.expect(httpClient.run((HttpUriRequest) EasyMock.anyObject())).andAnswer(new IAnswer<Function<Function<HttpResponse, Object>, Either<Exception, Object>>>() {

        @Override
        public Function<Function<HttpResponse, Object>, Either<Exception, Object>> answer() throws Throwable {
            HttpUriRequest req = (HttpUriRequest) EasyMock.getCurrentArguments()[0];
            return StandAloneTrustedHttpClientImpl.run(httpClient, req);
        }
    }).anyTimes();
    EasyMock.replay(httpClient);
    defaultOrganization = new DefaultOrganization();
    User anonymous = new JaxbUser("anonymous", "test", defaultOrganization, new JaxbRole(DefaultOrganization.DEFAULT_ORGANIZATION_ANONYMOUS, defaultOrganization));
    UserDirectoryService userDirectoryService = EasyMock.createMock(UserDirectoryService.class);
    EasyMock.expect(userDirectoryService.loadUser((String) EasyMock.anyObject())).andReturn(anonymous).anyTimes();
    EasyMock.replay(userDirectoryService);
    service.setUserDirectoryService(userDirectoryService);
    Organization organization = new DefaultOrganization();
    OrganizationDirectoryService organizationDirectoryService = EasyMock.createMock(OrganizationDirectoryService.class);
    EasyMock.expect(organizationDirectoryService.getOrganization((String) EasyMock.anyObject())).andReturn(organization).anyTimes();
    EasyMock.replay(organizationDirectoryService);
    service.setOrganizationDirectoryService(organizationDirectoryService);
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getUser()).andReturn(anonymous).anyTimes();
    EasyMock.expect(securityService.getOrganization()).andReturn(organization).anyTimes();
    EasyMock.replay(securityService);
    service.setSecurityService(securityService);
    serviceRegistry = new ServiceRegistryInMemoryImpl(service, securityService, userDirectoryService, organizationDirectoryService, EasyMock.createNiceMock(IncidentService.class));
    service.setServiceRegistry(serviceRegistry);
    service.setTrustedHttpClient(httpClient);
    final Workspace workspace = EasyMock.createNiceMock(Workspace.class);
    service.setWorkspace(workspace);
    EasyMock.expect(workspace.get((URI) EasyMock.anyObject())).andAnswer(new IAnswer<File>() {

        @Override
        public File answer() throws Throwable {
            final URI uri = (URI) EasyMock.getCurrentArguments()[0];
            final String[] pathElems = uri.getPath().split("/");
            final String file = pathElems[pathElems.length - 1];
            return new File(mediaPackageRoot, file);
        }
    }).anyTimes();
    EasyMock.replay(workspace);
    BundleContext bc = EasyMock.createNiceMock(BundleContext.class);
    EasyMock.expect(bc.getProperty("org.opencastproject.download.directory")).andReturn(distributionRoot.toString()).anyTimes();
    EasyMock.expect(bc.getProperty("org.opencastproject.download.url")).andReturn(UrlSupport.DEFAULT_BASE_URL).anyTimes();
    ComponentContext cc = EasyMock.createNiceMock(ComponentContext.class);
    Dictionary<String, Object> p = new Hashtable<String, Object>();
    p.put(DistributionService.CONFIG_KEY_STORE_TYPE, "download");
    EasyMock.expect(cc.getProperties()).andReturn(p).anyTimes();
    EasyMock.expect(cc.getBundleContext()).andReturn(bc).anyTimes();
    EasyMock.replay(bc, cc);
    service.activate(cc);
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) Organization(org.opencastproject.security.api.Organization) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) JaxbUser(org.opencastproject.security.api.JaxbUser) URI(java.net.URI) Function(org.opencastproject.util.data.Function) SecurityService(org.opencastproject.security.api.SecurityService) Either(org.opencastproject.util.data.Either) ServiceRegistryInMemoryImpl(org.opencastproject.serviceregistry.api.ServiceRegistryInMemoryImpl) TrustedHttpClient(org.opencastproject.security.api.TrustedHttpClient) ComponentContext(org.osgi.service.component.ComponentContext) Hashtable(java.util.Hashtable) HttpResponse(org.apache.http.HttpResponse) UserDirectoryService(org.opencastproject.security.api.UserDirectoryService) StatusLine(org.apache.http.StatusLine) IAnswer(org.easymock.IAnswer) JaxbRole(org.opencastproject.security.api.JaxbRole) File(java.io.File) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) OrganizationDirectoryService(org.opencastproject.security.api.OrganizationDirectoryService) Workspace(org.opencastproject.workspace.api.Workspace) BundleContext(org.osgi.framework.BundleContext) Before(org.junit.Before)

Aggregations

Function (org.opencastproject.util.data.Function)8 EntityManager (javax.persistence.EntityManager)3 File (java.io.File)2 URI (java.net.URI)2 EntityTransaction (javax.persistence.EntityTransaction)2 TrustedHttpClient (org.opencastproject.security.api.TrustedHttpClient)2 Either (org.opencastproject.util.data.Either)2 Option (org.opencastproject.util.data.Option)2 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Hashtable (java.util.Hashtable)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 IAnswer (org.easymock.IAnswer)1 Before (org.junit.Before)1 Test (org.junit.Test)1 SearchResultItem (org.opencastproject.oaipmh.persistence.SearchResultItem)1