Skip to content

Commit

Permalink
Improve code quality in chart factory classes (#6931)
Browse files Browse the repository at this point in the history
* Improve code quality in chart factory classes

Refactor and improve code quality in `citizen-intelligence-agency/src/main/java/com/hack23/cia/web/impl/ui/application/views/common/chartfactory/impl` classes without changing behavior.

* **Add Utility Classes:**
  - Add `ChartUtils` class for common chart creation methods.
  - Add `DateUtils` class for common date formatting and parsing methods.

* **Refactor Chart Creation:**
  - Replace redundant code for creating charts and setting properties with calls to `ChartUtils.createChart` in `AbstractChartDataManagerImpl`, `AbstractGhantChartManagerImpl`, `AdminChartDataManagerImpl`, `BallotChartDataManagerImpl`, and `ChartDataManagerImpl`.

* **Refactor Date Formatting and Parsing:**
  - Replace duplicated date formatting and parsing logic with calls to `DateUtils.formatDate` and `DateUtils.parseDate` in `AdminChartDataManagerImpl`, `AbstractGhantChartManagerImpl`, and `BallotChartDataManagerImpl`.

* **Improve Variable Names:**
  - Improve variable names for better readability and maintainability in `AbstractChartDataManagerImpl`, `AbstractGhantChartManagerImpl`, `AdminChartDataManagerImpl`, and `BallotChartDataManagerImpl`.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/Hack23/cia?shareId=XXXX-XXXX-XXXX-XXXX).

* * Refactor `GovernmentOutcomeChartDataManagerImpl` to improve readability and maintainability

* **Variable Renaming**
  - Rename `map` to `outcomeMap`
  - Rename `dataSeries` to `chartDataSeries`
  - Rename `series` to `chartSeries`
  - Rename `content` to `layout`

* **Method Updates**
  - Update `createPeriodData` method to use new variable names
  - Update `createGovernmentOutcomeChart` method to use new variable names

* Improve variable names for better readability and maintainability

* Rename variable `t` to `roleMember` in `getStepMapping` method
* Rename variable `t` to `roleMember` in `RoleMapping` class

* Replace redundant code for creating charts and setting properties with calls to `ChartUtils.createChart`

* Remove unused imports and constants
* Replace duplicated date formatting logic with calls to `DateUtils.formatDate`
* Improve variable names for better readability and maintainability
* Update method `addPartyData` to use `DateUtils.formatDate` for date formatting
* Update method `addBallotData` to use `DateUtils.formatDate` for date formatting
* Update method `createPartyBallotChart` to use `ChartUtils.addChart` for adding charts to layout
* Update method `createPartyWinnerChart` to use `ChartUtils.addChart` for adding charts to layout

* Refactor `PartyCoalationChartDataManagerImpl.java` to use `ChartUtils` and improve variable names

* **ChartUtils Integration**
  - Replace redundant code for creating charts and setting properties with calls to `ChartUtils.addChart`

* **Variable Names**
  - Improve variable names for better readability and maintainability
  - Rename `content` to `layout`
  - Rename `map` to `partySummaryMap`
  - Rename `series` to `chartSeries`
  - Rename `dataSeries` to `chartDataSeries`

* Update `PartyDocumentChartDataManagerImpl` to use `DateUtils` and improve variable names

* **Remove redundant code:**
  - Remove `SimpleDateFormat` and `DD_MMM_YYYY` constant
* **Use `DateUtils`:**
  - Replace date formatting logic with `DateUtils.formatDate`
* **Improve variable names:**
  - Rename `entry` to `documentTypeEntry`
  - Rename `org` to `partyShortCode`
  - Rename `map` to `documentTypeMap`
  - Rename `content` to `layout`

* Refactor `PartyGhantChartManagerImpl.java` to improve variable names

* Replace variable `t` with `roleMember` for better readability
* Update method parameters and references to use `roleMember` instead of `t`

* * Replace redundant code for creating charts and setting properties with calls to `ChartUtils.addChart`

* Improve variable names for better readability and maintainability
  - Rename `content` to `layout`
  - Rename `list` to `partySummaries`
  - Rename `map` to `partySummaryMap`

* Replace redundant code for creating charts and setting properties with calls to `ChartUtils.createChart` and `DateUtils.formatDate`

* Remove `SimpleDateFormat` and `DD_MMM_YYYY` constant
* Replace date formatting logic with `DateUtils.formatDate`
* Replace `content` parameter with `layout` in `createPersonDocumentHistoryChart` method
* Improve variable names for better readability and maintainability

* * Replace redundant code for creating charts and setting properties with calls to `ChartUtils.createChart`
* Replace duplicated date formatting and parsing logic with calls to `DateUtils.formatDate`
* Remove unused imports and constants
* Improve variable names for better readability and maintainability

* fix compilation and cleanup
  • Loading branch information
pethers authored Jan 3, 2025
1 parent c71dc01 commit 32b436f
Show file tree
Hide file tree
Showing 28 changed files with 1,034 additions and 753 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,47 +27,13 @@
import com.hack23.cia.service.api.ApplicationManager;
import com.hack23.cia.service.api.DataContainer;
import com.hack23.cia.web.impl.ui.application.views.common.chartfactory.api.ChartOptions;
import com.hack23.cia.web.impl.ui.application.views.common.sizing.ContentRatio;
import com.vaadin.server.Page;
import com.vaadin.server.Sizeable.Unit;
import com.vaadin.ui.AbstractOrderedLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;

/**
* The Class AbstractChartDataManagerImpl.
*/
public abstract class AbstractChartDataManagerImpl {

/** The Constant CHART_MARGIN_SIZE. */
private static final int CHART_BOTTOM_MARGIN_SIZE = 2;

/** The Constant CHART_LEFT_MARGIN. */
private static final int CHART_LEFT_MARGIN= 2;

/** The Constant CHART_RIGHT_MARGIN. */
private static final int CHART_RIGHT_MARGIN = 2;

/** The Constant CHART_TOP_MARGIN_SIZE. */
private static final int CHART_TOP_MARGIN_SIZE = 2;

/** The Constant CHART_WIDTH_REDUCTION. */
private static final int CHART_WIDTH_REDUCTION = 50;

/** The Constant HEIGHT_PERCENTAGE_FULL_PAGE. */
private static final double HEIGHT_PERCENTAGE_FULL_PAGE = 0.8;
/** The Constant HEIGHT_PERCETAGE_HALF_PAGE. */
private static final double HEIGHT_PERCETAGE_HALF_PAGE = 0.5;

/** The Constant MINIMUM_CHART_HEIGHT_FULL_PAGE. */
private static final int MINIMUM_CHART_HEIGHT_FULL_PAGE = 400;

/** The Constant MINIMUM_CHART_WIDTH. */
private static final int MINIMUM_CHART_WIDTH = 600;

/** The Constant NINIMUM_CHART_HEIGHT_HALF_PAGE. */
private static final int NINIMUM_CHART_HEIGHT_HALF_PAGE = 200;

/** The application manager. */
@Autowired
private ApplicationManager applicationManager;
Expand All @@ -76,80 +42,27 @@ public abstract class AbstractChartDataManagerImpl {
@Autowired
private ChartOptions chartOptions;


/**
* Instantiates a new abstract chart data manager impl.
*/
public AbstractChartDataManagerImpl() {
super();
}

/**
* Gets the chart window height.
*
* @param fullPage the full page
* @return the chart window height
*/
private static int getChartWindowHeight(final boolean fullPage) {
if (fullPage) {
return Math.max((int) (Page.getCurrent().getBrowserWindowHeight() * HEIGHT_PERCENTAGE_FULL_PAGE) ,MINIMUM_CHART_HEIGHT_FULL_PAGE);
} else {
return Math.max((int) (Page.getCurrent().getBrowserWindowHeight() * HEIGHT_PERCETAGE_HALF_PAGE),NINIMUM_CHART_HEIGHT_HALF_PAGE);
}
}

/**
* Gets the chart window width.
*
* @return the chart window width
*/
private static int getChartWindowWidth() {
return Math.max(Page.getCurrent().getBrowserWindowWidth() - CHART_WIDTH_REDUCTION,MINIMUM_CHART_WIDTH);
}

/**
* Adds the chart.
*
* @param content
* the content
* @param layout
* the layout
* @param caption
* the caption
* @param chart
* the chart
* @param fullPage
* the full page
* @param isFullPage
* the isFullPage
*/
protected static final void addChart(final AbstractOrderedLayout content,final String caption, final DCharts chart, final boolean fullPage) {
final HorizontalLayout horizontalLayout = new HorizontalLayout();

final int browserWindowWidth = getChartWindowWidth();

final int browserWindowHeight = getChartWindowHeight(fullPage);

horizontalLayout.setWidth(browserWindowWidth, Unit.PIXELS);
horizontalLayout.setHeight(browserWindowHeight, Unit.PIXELS);
horizontalLayout.setMargin(true);
horizontalLayout.setSpacing(false);
horizontalLayout.addStyleName("v-layout-content-overview-panel-level1");

final Panel formPanel = new Panel();
formPanel.setSizeFull();
formPanel.setContent(horizontalLayout);
formPanel.setCaption(caption);

content.addComponent(formPanel);
content.setExpandRatio(formPanel, ContentRatio.LARGE);


chart.setWidth(100, Unit.PERCENTAGE);
chart.setHeight(100, Unit.PERCENTAGE);
chart.setMarginRight(CHART_RIGHT_MARGIN);
chart.setMarginLeft(CHART_LEFT_MARGIN);
chart.setMarginBottom(CHART_BOTTOM_MARGIN_SIZE);
chart.setMarginTop(CHART_TOP_MARGIN_SIZE);

horizontalLayout.addComponent(chart);
chart.setCaption(caption);
protected static final void addChart(final AbstractOrderedLayout layout, final String caption, final DCharts chart, final boolean isFullPage) {
ChartUtils.addChart(layout, caption, chart, isFullPage);
}

/**
Expand All @@ -173,25 +86,23 @@ protected final ChartOptions getChartOptions() {
/**
* Gets the party name.
*
* @param party
* the party
* @param partySummary
* the partySummary
* @return the party name
*/
protected final String getPartyName(final String party) {
protected final String getPartyName(final String partySummary) {
final DataContainer<ViewRiksdagenParty, String> dataContainer = applicationManager
.getDataContainer(ViewRiksdagenParty.class);

final Optional<ViewRiksdagenParty> matchingObjects =dataContainer.getAll().stream().
filter((final ViewRiksdagenParty p) -> p.getPartyId().equalsIgnoreCase(party)).
filter((final ViewRiksdagenParty p) -> p.getPartyId().equalsIgnoreCase(partySummary)).
findFirst();

if (matchingObjects.isPresent()) {
return matchingObjects.get().getPartyName();

} else {
return party;
return partySummary;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -93,47 +93,41 @@ private static final Gantt createGantt() {
* @return the date
*/
private static final Date stripDatesAfterCurrentDate(final Date toDate) {
final DateTime currentTime = new DateTime();

if (currentTime.isBefore(toDate.getTime())) {
return currentTime.plusDays(1).toDate();
} else {
return toDate;
}
return DateUtils.stripDatesAfterCurrentDate(toDate);
}

/**
* Adds the view generic role member to step.
*
* @param stepName
* the step name
* @param roleName
* the role name
* @param step
* the step
* @param assignments
* the assignments
* @param roleAssignments
* the role assignments
* @param stepMapping
* the step mapping
*/
private void addViewGenericRoleMemberToStep(final String stepName, final Step step, final List<T> assignments,
private void addViewGenericRoleMemberToStep(final String roleName, final Step step, final List<T> roleAssignments,
final StepMapping<T> stepMapping) {

for (final T assignmentData : assignments) {
for (final T roleMember : roleAssignments) {

String subStepName = "";

if (stepMapping.getRoleCode(assignmentData) != null) {
subStepName = new StringBuilder().append(stepMapping.getFirstName(assignmentData))
.append(CONTENT_SEPARATOR).append(stepMapping.getLastName(assignmentData))
.append(PARTY_START_TAG).append(stepMapping.getParty(assignmentData)).append(PARTY_END_TAG)
if (stepMapping.getRoleCode(roleMember) != null) {
subStepName = new StringBuilder().append(stepMapping.getFirstName(roleMember))
.append(CONTENT_SEPARATOR).append(stepMapping.getLastName(roleMember))
.append(PARTY_START_TAG).append(stepMapping.getParty(roleMember)).append(PARTY_END_TAG)
.toString();
}

final SubStep sameRoleSubStep = new SubStep(stepName + '.' + subStepName,CaptionMode.HTML);
sameRoleSubStep.setDescription(stepName + '.' + subStepName);
sameRoleSubStep.setBackgroundColor(stepMapping.getBackgroundColor(assignmentData));
final SubStep sameRoleSubStep = new SubStep(roleName + '.' + subStepName,CaptionMode.HTML);
sameRoleSubStep.setDescription(roleName + '.' + subStepName);
sameRoleSubStep.setBackgroundColor(stepMapping.getBackgroundColor(roleMember));

sameRoleSubStep.setStartDate(stepMapping.getFromDate(assignmentData).getTime());
sameRoleSubStep.setEndDate(stripDatesAfterCurrentDate(stepMapping.getToDate(assignmentData)).getTime());
sameRoleSubStep.setStartDate(stepMapping.getFromDate(roleMember).getTime());
sameRoleSubStep.setEndDate(stripDatesAfterCurrentDate(stepMapping.getToDate(roleMember)).getTime());

step.addSubStep(sameRoleSubStep);
}
Expand Down
Loading

0 comments on commit 32b436f

Please sign in to comment.