* @param plan
* @return
*/
private PlanStatistics generateStatistics(Plan plan) {
PlanProperties properties = plan.getPlanProperties();
ProjectBasis basis = plan.getProjectBasis();
SampleRecordsDefinition samples = plan.getSampleRecordsDefinition();
long numSamples = samples.getRecords().size();
List<Leaf> leaves = plan.getTree().getRoot().getAllLeaves();
long numLeaves = leaves.size();
long numMappedLeaves = 0;
long numMeasuresNeededTotal = 0;
long numEvaluated = 0;
long numAlternatives = plan.getAlternativesDefinition().getAlternatives().size();
long numConsideredAlternatives = plan.getAlternativesDefinition().getConsideredAlternatives().size();
// determine the number of users who have been working on the plan
ChangelogStatistics changelogStatistics = new ChangelogStatistics();
plan.handleChanges(changelogStatistics);
int numDistinctUsers = changelogStatistics.getNumberOfUsers();
double percentageDefinedTransformers = 0.0;
List<String> consideredAltNames = new ArrayList<String>();
for (Alternative alt : plan.getAlternativesDefinition().getConsideredAlternatives()) {
consideredAltNames.add(alt.getName());
}
List<ValidationError> errors = new ArrayList<ValidationError>();
for (Iterator<Leaf> iter = leaves.iterator(); iter.hasNext();) {
Leaf l = iter.next();
if (l.isMapped()) {
numMappedLeaves++;
}
int numMeasuresNeeded = 0;
if (l.isSingle()) {
numMeasuresNeeded += numConsideredAlternatives;
} else {
numMeasuresNeeded += (numConsideredAlternatives * numSamples);
}
numMeasuresNeededTotal += numMeasuresNeeded;
for (Iterator<String> alts = consideredAltNames.iterator(); alts.hasNext();) {
String alt = alts.next();
Values values = l.getValues(alt);
if (values != null) {
for (Iterator<Value> valueiter = values.getList().iterator(); valueiter.hasNext();) {
Value value = valueiter.next();
if ((value != null) && (value.getScale() != null) && value.isEvaluated()) {
numEvaluated++;
}
}
}
}
if (l.getTransformer() != null) {
if (l.isCompletelyTransformed(errors)) {
percentageDefinedTransformers += 1;
}
}
}
percentageDefinedTransformers = percentageDefinedTransformers / numLeaves;
double percentagePopulatedValues = (numMeasuresNeededTotal == 0) ? 0.0 : numEvaluated
/ (double) numMeasuresNeededTotal;
String creatorUsername = properties.getOwner();
String creatorEmail = null;
try {
if (em != null) {
creatorEmail = em.createQuery("select email from User where username = :username", String.class)
.setParameter("username", creatorUsername).getSingleResult();
}
} catch (Exception e) {
LOGGER.debug("Failed to retrieve creator email.", e);
}
int numPlansCreated = 0;
try {
if (em != null) {
numPlansCreated = (em.createQuery("select count(*) from PlanProperties p where p.owner = :username",
Long.class).setParameter("username", creatorUsername).getSingleResult()).intValue();
}
} catch (Exception e) {
LOGGER.debug("Failed to determine number of plans created", e);
}
Date decisionOn = null;
if (plan.getRecommendation().getAlternative() != null) {
decisionOn = new Date(plan.getRecommendation().getChangeLog().getChanged()); // decision
// on
}
PlanStatistics statistics = new PlanStatistics(
plan.getId(),
properties.getId(),
creatorUsername,
properties.getAuthor(),
creatorEmail,
properties.getName(),
properties.getState().getValue(), // "status"
new Date(plan.getChangeLog().getCreated()), // created on
decisionOn, // decision on
properties.getChangeLog().getChanged(), // "stage last accessed"
properties.getState().getValue(), // "highest stage achieved"
numSamples, // # samples
numLeaves, // # leaves
numMappedLeaves, // # mapped leaves
numMeasuresNeededTotal, // # measures needed
numAlternatives, // # alternatives
StringUtils.length(basis.getDocumentTypes()), StringUtils.length(properties.getDescription()),
StringUtils.length(basis.getMandate()), StringUtils.length(basis.getPlanningPurpose()),
StringUtils.length(basis.getDesignatedCommunity()), StringUtils.length(basis.getApplyingPolicies()),
StringUtils.length(basis.getOrganisationalProcedures()), StringUtils.length(basis.getPreservationRights()),
StringUtils.length(basis.getReferenceToAgreements()), StringUtils.length(basis.getPlanRelations()),
StringUtils.length(basis.getTriggers().getNewCollection().getDescription()), StringUtils.length(basis
.getTriggers().getPeriodicReview().getDescription()), StringUtils.length(basis.getTriggers()
.getChangedEnvironment().getDescription()), StringUtils.length(basis.getTriggers()
.getChangedObjective().getDescription()), StringUtils.length(basis.getTriggers()
.getChangedCollectionProfile().getDescription()), StringUtils.length(samples.getCollectionProfile()
.getDescription()), StringUtils.length(samples.getCollectionProfile().getTypeOfObjects()),
StringUtils.length(samples.getCollectionProfile().getExpectedGrowthRate()), StringUtils.length(samples
.getCollectionProfile().getRetentionPeriod()), StringUtils.length(samples.getSamplesDescription()),
StringUtils.length(plan.getRequirementsDefinition().getDescription()),