accounting-dashboard-harves.../src/main/java/org/gcube/dataharvest/harvester/VreUsersHarvester.java

123 lines
4.1 KiB
Java

package org.gcube.dataharvest.harvester;
import java.io.IOException;
import java.net.MalformedURLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import org.gcube.dataharvest.datamodel.Harvest;
import org.gcube.dataharvest.utils.Utils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class VreUsersHarvester extends BasicHarvester {
// private final String CATEGORY_NAME = "Accounting";
private static Logger logger = LoggerFactory.getLogger(VreUsersHarvester.class);
public VreUsersHarvester(String start, String end) throws ParseException {
super(start, end);
}
public VreUsersHarvester(Date start, Date end) throws ParseException {
super(start, end);
}
@Override
public List<Harvest> getData() throws Exception {
ArrayList<Harvest> data = new ArrayList<Harvest>();
Properties props = readServiceEndpoint();
String[] vres = getActiveVREs(true);
for (String vre : vres) {
String key = vre.substring(vre.lastIndexOf("/") + 1);
String token = props.getProperty(key);
try {
int measure = get(token);
Harvest harvest = new Harvest(Harvest.USERS, vre, measure);
data.add(harvest);
logger.debug(harvest.toString());
} catch (Exception x) {
logger.error("VreUsersHarvester::getData. " + vre + ". " + x.getLocalizedMessage());
}
}
return data;
}
private int get(String token) throws MalformedURLException, IOException {
int userNumber = 0;
// la seguente stringa deve essere letta dinamicamente
String url = "https://socialnetworking1.d4science.org/social-networking-library-ws/rest/2/users/get-all-usernames?gcube-token=";
JSONObject jsonObject = new JSONObject(Utils.getJson(url + token));
Boolean success = (Boolean) jsonObject.get("success");
if (success == false) {
String message = "get-all-usernames returned an error. token: " + token;
logger.error(message);
throw new IOException(message);
}
userNumber = jsonObject.getJSONArray("result").length();
return userNumber;
}
// private Properties readServiceEndpoint() throws Exception {
// Properties props = new Properties();
// String scope = "/d4science.research-infrastructures.eu";
// ScopeProvider.instance.set("/d4science.research-infrastructures.eu");
//
// SimpleQuery query = queryFor(ServiceEndpoint.class);
// query.addCondition("$resource/Profile/Name/text() eq '" +
// RUNTIME_RESOURCE_NAME + "'");
// query.addCondition("$resource/Profile/Category/text() eq '" +
// CATEGORY_NAME + "'");
//
// DiscoveryClient<ServiceEndpoint> client =
// clientFor(ServiceEndpoint.class);
// String password = null;
// try {
// List<ServiceEndpoint> list = client.submit(query);
// if (list.size() > 1) {
// logger.error("Too many Service Endpoints having name " +
// RUNTIME_RESOURCE_NAME
// + " in this scope having Category " + CATEGORY_NAME);
// } else if (list.size() == 0) {
// logger.error("There is no Service Endpoint having name " +
// RUNTIME_RESOURCE_NAME
// + " and Category " + CATEGORY_NAME + " in this scope " + scope);
// } else {
// for (ServiceEndpoint res : list) {
// AccessPoint[] accessPoints = (AccessPoint[]) res.profile().accessPoints()
// .toArray(new AccessPoint[res.profile().accessPoints().size()]);
// for (AccessPoint found : accessPoints) {
// password =
// org.gcube.common.encryption.StringEncrypter.getEncrypter().decrypt(found.password());
// for (ServiceEndpoint.Property prop : found.properties()) {
// props.setProperty(prop.name(),
// org.gcube.common.encryption.StringEncrypter.getEncrypter().decrypt(prop.value()));
// }
// }
//
// }
// }
// return props;
// } catch (Exception e) {
// logger.error(e.getLocalizedMessage());
// throw e;
// }
// }
// public static void main(String[] argv) {
// try {
// VreUsersHarvester a = new VreUsersHarvester("2017-01-01", "2017-02-01");
// List<Harvest> list = a.getData();
// for(Harvest l : list) {
// System.out.println(l.toString());
// }
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
}