use of org.apache.isis.applib.ApplicationException in project estatio by estatio.
the class OccupancyImport method fetchLease.
private Lease fetchLease(final String leaseReference) {
final Lease lease;
lease = leaseRepository.findLeaseByReference(leaseReference.trim().replaceAll("~", "+"));
if (lease == null) {
throw new ApplicationException(String.format("Lease with reference %s not found.", leaseReference));
}
return lease;
}
use of org.apache.isis.applib.ApplicationException in project estatio by estatio.
the class OccupancyImport method importData.
@Override
@Programmatic
public List<Object> importData(Object previousRow) {
final Lease lease = fetchLease(leaseReference);
final Unit unit = unitRepository.findUnitByReference(unitReference);
if (unitReference != null && unit == null) {
throw new ApplicationException(String.format("Unit with reference %s not found.", unitReference));
}
Occupancy occupancy = occupancyRepository.findByLeaseAndUnitAndStartDate(lease, unit, startDate);
if (occupancy == null) {
occupancy = occupancyRepository.newOccupancy(lease, unit, startDate);
}
occupancy.setEndDate(endDate);
occupancy.setUnitSizeName(size);
occupancy.setBrandName(brand != null ? brand.replaceAll("\\p{C}", "").trim() : null, null, null);
occupancy.setSectorName(sector);
occupancy.setActivityName(activity);
occupancy.setReportTurnover(reportTurnover != null ? Occupancy.OccupancyReportingType.valueOf(reportTurnover) : Occupancy.OccupancyReportingType.NO);
occupancy.setReportRent(reportRent != null ? Occupancy.OccupancyReportingType.valueOf(reportRent) : Occupancy.OccupancyReportingType.NO);
occupancy.setReportOCR(reportOCR != null ? Occupancy.OccupancyReportingType.valueOf(reportOCR) : Occupancy.OccupancyReportingType.NO);
return Lists.newArrayList(occupancy);
}
use of org.apache.isis.applib.ApplicationException in project estatio by estatio.
the class UrlDownloaderUsingNtlmCredentials method download.
@Override
public byte[] download(final URL url) throws IOException {
HttpHost target = new HttpHost(url.getHost(), url.getPort(), url.getProtocol());
// Make sure the same context is used to execute logically related requests
// (not thread-safe, so need a new one each time)
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
HttpGet httpGet = new HttpGet(url.getFile());
try (final CloseableHttpResponse httpResponse = httpclient.execute(target, httpGet, context)) {
final StatusLine statusLine = httpResponse.getStatusLine();
if (statusLine == null) {
throw new ApplicationException(String.format("Could not obtain response statusLine for %s", url.toExternalForm()));
}
final int statusCode = statusLine.getStatusCode();
if (statusCode != 200) {
// try to read content of entity, but ignore any exceptions
// because we are simply trying to get extra data to report in the exception below
InputStreamReader inputStreamReader = null;
String entityContent = "";
try {
inputStreamReader = new InputStreamReader(httpResponse.getEntity().getContent());
entityContent = CharStreams.toString(inputStreamReader);
} catch (java.lang.Throwable ex) {
// ignore
} finally {
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (Exception ex) {
// ignore
}
}
}
throw new ApplicationException(String.format("Failed to download from '%s': %d %s\n%s", url.toExternalForm(), statusCode, statusLine.getReasonPhrase(), entityContent));
}
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
httpResponse.getEntity().writeTo(baos);
return baos.toByteArray();
}
}
Aggregations