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();
}
};
}
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);
}
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);
}
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();
}
};
}
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);
}
Aggregations