Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

558587 implement parametrized customer export #87

Merged
merged 6 commits into from
Dec 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions bundles/org.eclipse.passage.loc.report.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ Bundle-Name: %Bundle-Name
Bundle-Vendor: %Bundle-Vendor
Bundle-Copyright: %Bundle-Copyright
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.eclipse.passage.loc.yars.api;bundle-version="0.1.0",
org.eclipse.passage.lic.users;bundle-version="0.5.0"
Export-Package: org.eclipse.passage.loc.report.internal.core;x-internal:=true
Require-Bundle: org.eclipse.passage.loc.yars.api;bundle-version="0.1.0";visibility:=reexport,
org.eclipse.passage.lic.users;bundle-version="0.5.0",
org.apache.commons.csv;bundle-version="1.4.0",
org.eclipse.osgi.services;bundle-version="3.8.0"
Export-Package: org.eclipse.passage.loc.report.internal.core;x-friends:="org.eclipse.passage.loc.report.core.tests"
Bundle-ActivationPolicy: lazy
Service-Component: OSGI-INF/org.eclipse.passage.loc.report.internal.core.Customers.xml,
OSGI-INF/org.eclipse.passage.loc.report.internal.core.CsvExportService.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.passage.loc.report.internal.core.CsvExportService">
<service>
<provide interface="org.eclipse.passage.loc.report.internal.core.ExportService"/>
</service>
<reference bind="installCustomerStorage" interface="org.eclipse.passage.loc.report.internal.core.CustomerStorage" name="installCustomerStorage"/>
<implementation class="org.eclipse.passage.loc.report.internal.core.CsvExportService"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.eclipse.passage.loc.report.internal.core.Customers">
<service>
<provide interface="org.eclipse.passage.loc.report.internal.core.CustomerStorage"/>
</service>
<reference bind="installUserRegistry" interface="org.eclipse.passage.lic.users.registry.UserRegistry" name="installUserRegistry"/>
<implementation class="org.eclipse.passage.loc.report.internal.core.Customers"/>
</scr:component>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*******************************************************************************
* Copyright (c) 2019 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.report.internal.core;

import java.io.IOException;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.eclipse.passage.loc.yars.internal.api.ListMedia;
import org.eclipse.passage.loc.yars.internal.api.ReportException;
import org.eclipse.passage.loc.yars.internal.api.Unsafe;

/**
* FIXME doc
*
* @since 0.1
*/
@SuppressWarnings("restriction")
final class Csv implements ListMedia<ProductCustomer> {
private final CSVPrinter stream;

public Csv(ExistingFileStream target, String... header) throws ReportException {
try {
stream = //
new CSVPrinter( //
target.stream(), //
CSVFormat.EXCEL //
.withHeader(header));
} catch (IOException e) {
throw new ReportException(Messages.getString("Csv.action_printer_construction_failure"), e); //$NON-NLS-1$
}
}

@Override
public final void start() throws ReportException {
}

@Override
public final void finish() throws ReportException {
unsafeCall(stream::close, Messages.getString("Csv.action_printer_closure_failure")); //$NON-NLS-1$
}

@Override
public final void startNode(ProductCustomer node) {
}

@Override
public final void finishNode(ProductCustomer node) throws ReportException {
unsafeCall(stream::println, Messages.getString("Csv.action_closing_row")); //$NON-NLS-1$
}

@Override
public final void inner(String data, String name) throws ReportException {
unsafeCall(() -> {
stream.print(data);
}, String.format(Messages.getString("Csv.action_filling_cell"), data, name)); //$NON-NLS-1$
}

private void unsafeCall(Unsafe<IOException> unsafe, String happening) throws ReportException {
try {
unsafe.call();
} catch (IOException e) {
throw new ReportException(String.format(Messages.getString("Csv.action_failed"), happening), e); //$NON-NLS-1$
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*******************************************************************************
* Copyright (c) 2019 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.report.internal.core;

import java.nio.file.Path;
import java.util.Set;

import org.eclipse.passage.loc.yars.internal.api.ReportException;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
* FIXME doc
*
* @since 0.1
*/
@SuppressWarnings("restriction")
@Component
public final class CsvExportService implements ExportService {

private CustomerStorage source;

@Override
public void exportCustomersForProducts(Set<String> products, Path target) throws ReportException {
new ProductCustomersToCsv(source).export(products, target);

}

@Override
@Reference
public void installCustomerStorage(CustomerStorage storage) {
source = storage;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*******************************************************************************
* Copyright (c) 2019 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.report.internal.core;

import java.util.Set;

import org.eclipse.passage.lic.users.UserDescriptor;
import org.eclipse.passage.lic.users.registry.UserRegistry;
import org.eclipse.passage.loc.yars.internal.api.Storage;

/**
* FIXME doc
*
* @since 0.1
*/
@SuppressWarnings("restriction")
public interface CustomerStorage extends Storage<UserDescriptor> {

Set<UserDescriptor> forProducts(Set<String> products);

void installUserRegistry(UserRegistry userRegistry);

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,31 @@
import org.eclipse.passage.lic.users.UserDescriptor;
import org.eclipse.passage.lic.users.UserLicenseDescriptor;
import org.eclipse.passage.lic.users.registry.UserRegistry;
import org.eclipse.passage.loc.yars.internal.api.Storage;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
* FIXME
*
* @since 0.1
*/
@SuppressWarnings("restriction")
public abstract class CustomerBase implements Storage<UserDescriptor> {
private final UserRegistry registry;
@Component
public final class Customers implements CustomerStorage {

protected CustomerBase(UserRegistry registry) {
this.registry = registry;
}
private UserRegistry registry;

@Override
public Set<UserDescriptor> forProducts(Set<String> products) {
return StreamSupport.stream(registry.getUserLicenses().spliterator(), false)//
.filter(lic -> products.contains(lic.getProductIdentifier())) //
.map(UserLicenseDescriptor::getUser) //
.collect(Collectors.toSet());
}

@Override
@Reference
public void installUserRegistry(UserRegistry userRegistry) {
this.registry = userRegistry;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
import org.eclipse.passage.loc.yars.internal.api.FetchedData;

/**
* FIXME
* FIXME doc
*
* @since 0.1
*/
@SuppressWarnings("restriction")
public final class CustomersFetch implements FetchedData<CustomerBase, ProductCustomer> {
final class CustomersFetch implements FetchedData<CustomerStorage, ProductCustomer> {

private final CustomerBase source;
private final CustomerStorage source;
private final Set<String> products;

public CustomersFetch(CustomerBase source, Set<String> products) {
public CustomersFetch(CustomerStorage source, Set<String> products) {
this.source = source;
this.products = products;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
import org.eclipse.passage.loc.yars.internal.api.Query;

/**
* FIXME
* FIXME doc
*
* @since 0.1
*/
@SuppressWarnings("restriction")
public final class CustomersForProductsQuery implements Query<CustomerBase, ProductCustomer, ProductNames> {
final class CustomersForProductsQuery implements Query<CustomerStorage, ProductCustomer, ProductNames> {

@Override
public String id() {
Expand All @@ -34,7 +34,7 @@ public String description() {
}

@Override
public FetchedData<CustomerBase, ProductCustomer> fetch(CustomerBase storage, ProductNames properties) {
public FetchedData<CustomerStorage, ProductCustomer> fetch(CustomerStorage storage, ProductNames properties) {
return new CustomersFetch(storage, properties.products());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*******************************************************************************
* Copyright (c) 2019 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.report.internal.core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.passage.loc.yars.internal.api.ReportException;

/**
* FIXME doc
*
* @since 0.1
*/
@SuppressWarnings("restriction")
final class ExistingFileStream {

private final Path path;

ExistingFileStream(Path path) {
this.path = path;
}

File file() throws ReportException {
if (Files.notExists(path)) {
return create().toFile();
} else if (Files.isDirectory(path)) {
throw new ReportException(
String.format("path %s points to a directory, but we expect a file to export to", path), null); //$NON-NLS-1$
}
return path.toFile();
}

Appendable stream() throws ReportException {
try {
return new OutputStreamWriter(new FileOutputStream(file()), StandardCharsets.UTF_8); // TODO: set encoding
} catch (FileNotFoundException e) {
throw new ReportException(String.format("file %s appears to be not-existing all of a sudden!", path), null); //$NON-NLS-1$
}
}

private Path create() throws ReportException {
try {
return Files.createFile(path);
} catch (IOException e) {
throw new ReportException(String.format("failed to create file %s", path), e); //$NON-NLS-1$
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*******************************************************************************
* Copyright (c) 2019 ArSysOp
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* ArSysOp - initial API and implementation
*******************************************************************************/
package org.eclipse.passage.loc.report.internal.core;

import java.nio.file.Path;
import java.util.Set;

import org.eclipse.passage.loc.yars.internal.api.ReportException;

/**
* FIXME doc
*
* FIXME: append the rest of over-customers export commands here
*
* @since 0.1
*/
@SuppressWarnings("restriction")
public interface ExportService {

void exportCustomersForProducts(Set<String> products, Path target) throws ReportException;

void installCustomerStorage(CustomerStorage stogage);
}
Loading