FOSSredder 0.5.0
Loading...
Searching...
No Matches
PropertyContractMatrix.h
Go to the documentation of this file.
1
6
7#pragma once
8
10#include "ExportConstants.h"
12
13#include <string>
14#include <unordered_map>
15#include <unordered_set>
16#include <vector>
17
19
24 std::vector<std::string> propertyNames;
25 std::vector<std::string> contractTypes;
26 std::unordered_map<std::string, std::unordered_map<std::string, double>>
28};
29
38inline std::string resolveContractType(
39 const std::string &contractId,
40 const std::unordered_map<std::string, std::string> &idToType,
42 const std::string trimmedContractId = core::utils::trim(contractId);
43 if (trimmedContractId.empty()) {
45 }
46
47 const auto indexedType = idToType.find(trimmedContractId);
48 if (indexedType != idToType.end()) {
49 const std::string trimmedType = core::utils::trim(indexedType->second);
50 if (!trimmedType.empty()) {
51 return trimmedType;
52 }
53 }
54
55 for (const auto &contract : state.contracts()) {
56 if (!contract) {
57 continue;
58 }
59 if (core::utils::trim(contract->id()) != trimmedContractId) {
60 continue;
61 }
62
63 const std::string trimmedType = core::utils::trim(contract->type());
64 if (!trimmedType.empty()) {
65 return trimmedType;
66 }
67 break;
68 }
69
71}
72
81 std::unordered_map<std::string, std::string> propertyNamesById;
82 propertyNamesById.reserve(state.properties().size());
83 for (const auto &property : state.properties()) {
84 if (property) {
85 propertyNamesById[property->id()] = property->name();
86 }
87 }
88
89 std::unordered_map<std::string, std::string> contractTypesById;
90 contractTypesById.reserve(state.contracts().size());
91 for (const auto &contract : state.contracts()) {
92 if (contract) {
93 contractTypesById[contract->id()] = contract->type();
94 }
95 }
96
98 std::unordered_set<std::string> seenContractTypes;
99 std::unordered_set<std::string> seenProperties;
100
101 for (const auto &transaction : state.transactions()) {
102 if (!transaction || transaction->propertyIds().empty()) {
103 continue;
104 }
105
106 const std::string contractType = resolveContractType(
107 transaction->contractId(), contractTypesById, state);
108 if (seenContractTypes.insert(contractType).second) {
109 matrix.contractTypes.push_back(contractType);
110 }
111
112 for (const auto &propertyId : transaction->propertyIds()) {
113 const auto propertyNameIt = propertyNamesById.find(propertyId);
114 const std::string propertyName = propertyNameIt != propertyNamesById.end()
115 ? propertyNameIt->second
116 : propertyId;
117 matrix.amountsByProperty[propertyName][contractType] +=
118 transaction->amount();
119 seenProperties.insert(propertyName);
120 }
121 }
122
123 matrix.propertyNames.reserve(seenProperties.size());
124 for (const auto &property : state.properties()) {
125 if (!property) {
126 continue;
127 }
128 if (!matrix.amountsByProperty.count(property->name())) {
129 continue;
130 }
131 if (seenProperties.erase(property->name()) > 0) {
132 matrix.propertyNames.push_back(property->name());
133 }
134 }
135
136 for (const auto &[propertyName, _] : matrix.amountsByProperty) {
137 if (seenProperties.erase(propertyName) > 0) {
138 matrix.propertyNames.push_back(propertyName);
139 }
140 }
141
142 return matrix;
143}
144
145} // namespace core::application::exporting::internal
Defines private constants shared by core export implementations.
Declares private string helpers shared across core implementation files.
Domain aggregate that stores catalog entities for one workspace.
const PropertyList & properties() const noexcept
Returns all properties in the catalog.
const TransactionList & transactions() const noexcept
Returns all transactions in the catalog.
const ContractList & contracts() const noexcept
Returns all contracts in the catalog.
PropertyContractMatrix buildPropertyContractMatrix(const core::domain::catalog::WorkspaceCatalog &state)
Builds the property/contract matrix from the current workspace snapshot.
std::string resolveContractType(const std::string &contractId, const std::unordered_map< std::string, std::string > &idToType, const core::domain::catalog::WorkspaceCatalog &state)
Resolves the final contract type label for one transaction.
std::string trim(std::string s)
Removes leading and trailing ASCII whitespace from a string.
Holds the matrix representation used by the CSV and XLSX exporters.
std::unordered_map< std::string, std::unordered_map< std::string, double > > amountsByProperty