> For the complete documentation index, see [llms.txt](https://admincode.gitbook.io/admincode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://admincode.gitbook.io/admincode/overview/publish-your-docs/solution.md).

# Solution

## Spring MVC and Spring Boot <a href="#id-49e7" id="id-49e7"></a>

**Spring MVC** is a Model-View-Controller framework. It is used to create scalable enterprise web applications. More complex than Spring Boot to setup and configure. If more flexibility is required with configuration, this is the best option.

**Spring Boot** is build on top of the Spring MVC. It minimizes the complexity of configuration and the boilerplate code that Spring MVC requires. It has build-in Jetty or Tomcat web server. It mostly used for rapid building of Microservice applications. Unlike Spring MVC, the layers it uses are, Presentation Layer, Data Access Layer, Service Layer, and Integration Layer.

## Web based application back-office system <a href="#id-67f5" id="id-67f5"></a>

## The Problem <a href="#ffaa" id="ffaa"></a>

But how to build a web application? What are the building blocks that make up the system?

## The Break Down <a href="#ab09" id="ab09"></a>

To solve this problem, we will have to split the solution into the most basic parts.

* Architecture design
* Authentication — Authorization — Audit Log
* User Interface

## **Architecture design** <a href="#e295" id="e295"></a>

As an MVC framework, Spring uses a **Model**, **View** and **Controller design approach**. The **Model** is the grouping of all entities, data, rules and logic that make up the system. The **View** represents the User Interface like HTML, JSP pages etc… And of course the **Controller**, which accepts input and converts it to commands for the **Model** or meaningful user data for the **View**.

To further extend the code readability and maintainability, we will make use of the Domain Driven Design in conjunction with the MVC design.

<figure><img src="https://miro.medium.com/v2/resize:fit:1400/1*LK88xUlOPuJpC5BHwFjpiA.png" alt="" height="414" width="700"><figcaption><p>Layers of DDD Architecture</p></figcaption></figure>

Starting from the bottom and going step by step up, lets see the **Model** in which the ***Domain Layer*** will reside. The ‘lowest-level’ class will be the entity, which represents a row from the database.

<figure><img src="https://miro.medium.com/v2/resize:fit:868/1*8tZR8NM5xQGKmOOHHg6t4A.png" alt="" height="632" width="434"><figcaption><p>Example of <strong>Article</strong> Entity class which extends <strong>BaseO</strong> class. <strong>BaseO</strong> class extends <strong>BaseEntity</strong> class</p></figcaption></figure>

All entities extend the parent abstract classes ***BaseO*** and ***BaseEntity***, which hold the global ‘parent’ attributes. Please note ***BaseO*** and ***BaseEntity*** as abstract classes, cannot and should NOT be instantiated.

At the next ‘upper-level’, the ***ArticleBO*** will hold the behavior and business logic of the entity.

<figure><img src="https://miro.medium.com/v2/resize:fit:790/1*osDHO8N3MdMn36G07ma28w.png" alt="" height="621" width="395"><figcaption><p>Example of <strong>ArticleBO</strong> which extends abstract <strong>BaseBO</strong> class</p></figcaption></figure>

**Business Object** classes, extend the abstract ***BaseBO*** class in order to inherit the CRUD behavior load | save | delete (*save method is used for both create and insert operations*).

Please notice the 2 concrete methods in ***BaseBO*** class, **loadBaseO()** and **saveBaseO()**. Both methods hold the basic functionality for loading and persisting an entity into the database. Both methods call the global ***IRepository*** interface which holds the responsibility of the ORM (Hibernate) functionality for load/save. In this way we satisfy the **Single Responsibility Principle** and **Dependency Inversion Principle.**

Coming up next, we will have the ***Service (Application) Layer***. This layer orchestrates the lower-level object business logic.

<figure><img src="https://miro.medium.com/v2/resize:fit:484/1*FuEB652K1cB7VzMqPjOLJA.png" alt="" height="378" width="242"><figcaption><p>Exampe <strong>CRUDService</strong> class which extends <strong>BaseService</strong> class</p></figcaption></figure>

```java
@Transactional
public void save(BaseDTO input) throws Exception {
   save(input, false);
}

public BaseDTO load(long id, Class<? extends BaseDTO> classType) throws Exception {
   BaseBO baseBo = getBussinessBeanFromClassType(classType);
   BaseO o = baseBo.load(id,    
   CustomModelMapper.mapDTOClassToModelClass(classType));
   if (o != null && o.getIsDeleted() == 1) return null;
   return CustomModelMapper.mapModeltoDTO(o, classType);
}
```

Please notice the method save() takes DTO (Data Transfer Object) as input, since this is a ***Service (Application) Layer**.* Load() method takes entity Id as parameter and returns an Entity converted to a DTO.

Provided, the system will have complex business rules and logic, lets use the [Facade³ ](https://patroclosdev.medium.com/design-patterns-facade-8d768f95f2cc)design pattern and provide a greater level of orchestration and abstraction.

<pre class="language-java"><code class="lang-java">@Component
public class Facade  {

@Autowired
private IProcessManager processManager;@Autowired
private CRUDProcess CRUDProcess;

public &#x3C;T> Object load(long id, Class&#x3C;? extends BaseDTO> inputType) throws Exception {
<strong>  IProcess&#x3C;?, ?, ?> process = b -> CRUDProcess.load(id, inputType);
</strong>  return processManager.runProcess(process, inputType, id, CRUDProcess.PROCESS_NAME_READ);
}

public Object delete(BaseDTO input) throws Exception {
  IProcess&#x3C;?, ?, ?> process = b -> CRUDProcess.delete(input);
  return processManager.runProcess(process, input, CRUDProcess.PROCESS_NAME_DELETE);
}

public Object saveNew(BaseDTO input) throws Exception {
  IProcess&#x3C;?, ?, ?> process = b -> CRUDProcess.saveNew(input);
  return processManager.runProcess(process, input, CRUDProcess.PROCESS_NAME_CREATE);
}

public Object saveUpdate(BaseDTO input) throws Exception {
<strong>  IProcess&#x3C;?, ?, ?> process = b -> CRUDProcess.saveUpdate(input);
</strong>  return processManager.runProcess(process, input, CRUDProcess.PROCESS_NAME_UPDATE);
}

}
</code></pre>

As you can see, the **Facade** class implements the CRUD functions and interacts with the corresponding ***Service (Application) Layer*** CRUD functions, by calling **ProcessManager.runProcess()** method.

```java
@Component
public class ProcessManager implements IProcessManager {

@Autowired
private ActivityProcess ActivityProcess;
@Autowired
private IAuthenticationService AuthenticationService;
@Autowired
private UserService UserService;

public Object runProcess(IProcess<?, ?, ?> process, Class<? extends BaseDTO> inputType, BaseDTO input, Long id, String processName) throws Exception{
   Object result = null;
   if (processName == null ) {
      throw new SystemException("ProcessName not defined");
   }

   var loggedUser = AuthenticationService.getLoggedDbUserDTO();
   if (loggedUser.getIsDeleted() == 1) {
      throw new SystemException("Process authorization failed. User is deleted");
   }

   if (loggedUser.getEnabled() != 1) {
      throw new SystemException("Process authorization failed. User is not enabled");
   }

   if (loggedUser.getRoles() == null) {
       throw new SystemException("Process authorization failed. User has no defined roles");
   }

   var authorities = UserService.getUserAuthorities(loggedUser);
   if (authorities == null) {
       throw new SystemException("Process authorization failed. No access permissions found");
   }

  var authoritiesList = authorities.stream().map(a -> a.getAuthority()).collect(Collectors.toList());
  if (!authoritiesList.contains(processName)) {
     throw new SystemException("Process authorization failed. User has no access to this process. No access permissions found");
  }

   boolean isProcessSuccess = false;
   String processError = "";
   String processId = ProcessUtil.getActivityProcessId();
   try
   {
     Thread.currentThread().setName(processId);
     result = process.run(input);
     isProcessSuccess = true;
   }
   catch (Exception e){
      processError = e.getMessage();
      if (e.getCause() != null) processError = e.getCause().getMessage();
      throw new SystemException(e);
   }
   finally {
      //Handle process failure code ...//reset thread's name after process and activity log insert is completeThread.currentThread().setName("");
   }

return  result;

}

}
```

**Problem**

Each process which is called from the **Facade**, needs to be **authorized** and handled, success or fail, with the same way across all the application.

Why don’t we call the **Service (Application)Layer** class method directly ?

We will need to write additional, duplicate code, each time a new process is added to the **Facade** class, resulting in possible implementation inconsistencies from the developers side. Plus we will have unnecessary boilerplate code.

**Solution**

Use **Strategy Pattern**

From the **ProcessManager** class above, we can pass the process implementation (**IProcess**) in the runProcess method().

Process Implementation

```java
@Component
public class CRUDProcess extends BaseProcess {

@Autowired
private CRUDService CRUDService;

public final String PROCESS_NAME_CREATE = "CREATE";
public final String PROCESS_NAME_READ = "READ";
public final String PROCESS_NAME_UPDATE = "UPDATE";
public final String PROCESS_NAME_DELETE = "DELETE";
public final String PROCESS_NAME_CANCEL = "CANCEL";

public BaseDTO load(long id, Class<? extends BaseDTO> input) throws Exception {
   BaseDTO dto = CRUDService.load(id, input);
   return dto;
}

public Object delete(BaseDTO input) throws Exception {
   CRUDService.delete(input);
   return input;
}

public Object saveNew(BaseDTO input) throws Exception {
   return save(input);
}

public Object saveUpdate(BaseDTO input) throws Exception {
   return save(input);
}

public Object save(BaseDTO input) throws Exception {
  CRUDService.save(input);
  return CRUDService.load(input.getId(), input.getClass());
}

public Object cancel(BaseDTO input) {
  return input;
}

}
```

Each process which is called from the **Facade**, is being authorized and handled, with success or failure, with the same way across all the application. In this way we satisfy the [**Open-Close Principle**](https://patroclosdev.medium.com/solid-the-5-principles-of-object-oriented-design-676095960598)⁴ as well. Moreover, for any changes needed in the **ProcessManager**, are easy to be implemented as we need to modify the code only in one place.

<figure><img src="https://miro.medium.com/v2/resize:fit:1400/1*W3hPvdVRWoJe3m8kKbGHFw.png" alt="" height="337" width="700"><figcaption><p>Example Detail Page when loading Article DTO</p></figcaption></figure>

## **Authentication — Authorization — Audit Log** <a href="#d2c1" id="d2c1"></a>

For authentication, Spring Security Configuration is used

<figure><img src="https://miro.medium.com/v2/resize:fit:1400/1*wJAt2-gJj9ps1JgQ2yaGyg.png" alt="" height="372" width="700"><figcaption></figcaption></figure>

*Sample Security Configuration **SecurityFilterChain** Bean (Spring MVC version 5)*

```java
@EnableWebSecurity
@Configuration
public class SecurityConfiguration {

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

    http.authorizeRequests().antMatchers("/login", "/signup", "/signupconfirm").permitAll()
    .antMatchers("/**", "/index**").access("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
    .and()
    .formLogin().loginPage("/login").defaultSuccessUrl("/index?page=dashboard")
       .successHandler(authenticationSuccessHandler())
    .failureHandler(authenticationFailureHandler())
    .and()
    .logout().logoutUrl("/logout").logoutSuccessHandler(logoutSuccessHandler())
        .invalidateHttpSession(true).deleteCookies("JSESSIONID")
    .and()
    .sessionManagement(session -> session
        .maximumSessions(1) //set maximum concurrent user logins
        .maxSessionsPreventsLogin(true) //second login will be prevented
        )
    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED).sessionFixation().migrateSession(); //on authentication create a new session to avoid session fixation attackhttp.csrf();http.headers()
    .frameOptions()
    .sameOrigin()
    .xssProtection()
    .block(false);

return http.build();

}

}
```

As we explained earlier, the **Process Manager** is authorizing each user’s permission to execute a process.

Additionally, from the **Process Manager, Audit Logs** are generated for every process call.

Additionally, [**Hibernate Envers**](https://hibernate.org/orm/envers/), is being used, to keeps historical audit tables with the information of modified entity fields.

<figure><img src="https://miro.medium.com/v2/resize:fit:1400/1*8eE4zk6upnaw6jlgISbExw.png" alt="" height="331" width="700"><figcaption></figcaption></figure>

<figure><img src="https://miro.medium.com/v2/resize:fit:1400/1*lyD8t5ikh4uSelVPbuRqOw.png" alt="" height="316" width="700"><figcaption><p>Example of an Audit Log for an edit made by user1 on Article entity</p></figcaption></figure>

## **User Interface** <a href="#bb97" id="bb97"></a>

[**Bootstrap**](https://getbootstrap.com/) version 5 is used for the frontend development of the pages. [**Thymeleaf**](https://www.thymeleaf.org/) is the preferred template engine in this project.

As a high level explanation of the User Interface, the application consists of the ***Search*** and ***Detail*** pages.

Search Page

<figure><img src="https://miro.medium.com/v2/resize:fit:1400/1*7IavB2W-smhvPxwzcejETw.png" alt="" height="334" width="700"><figcaption><p>Example of Articles search page</p></figcaption></figure>

Detail Page

<figure><img src="https://miro.medium.com/v2/resize:fit:1400/1*3lMAfmRYn-xMSMTFoIHJpQ.png" alt="" height="319" width="700"><figcaption><p>Example of Article Detail page</p></figcaption></figure>

**Problem**

Search and Detail Pages need to be consistent and also minimize the boilerplate code needed when developing new pages. Standard Components HTML code generation so they can be modified with the minimum effort.

**Solution**

Create a **masterlayout.html** page which is used as a parent-master page for all pages. This page will include all the **css** and **javascripts** used by the UI.

In extend, to standardize the search pages, we create a **summarylayout.html** page which is used a parent-master for the search pages. Which is also using the above **masterlayout.html** as parent layout.

In addition, to standardize the detail pages, we create a **pagelayout.html** page which is used a parent-master for the search pages. Which is also using the above **masterlayout.html** as parent layout.

**Controllers,** extend parent classes, as the example below of the **ArticlesController (Articles search page)**.

<figure><img src="https://miro.medium.com/v2/resize:fit:722/1*o1iXlL4YPYOhBimUXkz2MA.png" alt="" height="982" width="361"><figcaption><p>Example of <strong>ArticlesController</strong> which extends <strong>SummaryController</strong> which extends <strong>BaseController</strong></p></figcaption></figure>

This way upgrading to a newer Bootstrap version and/or modifying CSS, Javascripts is made easy since they can be changed from one place.

**Controller** classes are extending behavior from parent classes, thus sharing common, reusable code to all the child subclasses.

**Problem**

How to change the ***look and feel*** of the entire application with the minimum effort?

**Solution**

Lets say we need to add a feature to all the input boxes across all the application. If we try to change all the input boxes from all the pages, this will take a considerable amount of time. So the approach we need to take is for the Components HTML code, to be generated from java code (UIComponent Classes), and added to the **ModelAndView** object, returned from the Controller class. In this way we achieve reusable and maintainable code which is easy to modify.

Making use of the thymeleaf templating, as explained above, all the pages extend a master page layout, thus is easy to modify the HTML, JSP pages along with the code changes.

<br>
