Skip to content

Commit

Permalink
Adding transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Polyarus committed Jul 31, 2023
1 parent 3373a40 commit b96662a
Show file tree
Hide file tree
Showing 15 changed files with 201 additions and 117 deletions.
4 changes: 2 additions & 2 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="lib" path="WEB-INF/lib/classgraph.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="src/"/>
<classpathentry kind="src" path="target/generated-sources/annotations"/>
<classpathentry kind="lib" path="WEB-INF/classes" sourcepath="WEB-INF/classes"/>
<classpathentry exported="true" kind="lib" path="WEB-INF/lib/jjwt-api.jar"/>
Expand All @@ -25,5 +24,6 @@
<classpathentry exported="true" kind="lib" path="WEB-INF/lib/hibernate-validator.jar"/>
<classpathentry exported="true" kind="lib" path="WEB-INF/lib/jboss-logging.jar"/>
<classpathentry exported="true" kind="lib" path="WEB-INF/lib/classmate.jar"/>
<classpathentry exported="true" kind="lib" path="WEB-INF/lib/classgraph.jar"/>
<classpathentry kind="output" path="WEB-INF/classes"/>
</classpath>
2 changes: 1 addition & 1 deletion .openapi-generator/openapi.yaml-default.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
aeae9bb3bcc568a07bfd671bde2b754790d021e0ba78d30453012414a00557fd
a3529c680086e88233c896b65e6731655c562ffe9ec6f072803f4fc68f5c11c0
8 changes: 0 additions & 8 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ info:
name: Apache-2.0
url: 'https://www.apache.org/licenses/LICENSE-2.0.html'

security:
- bearerAuth: []

tags:
- name: Common
Expand Down Expand Up @@ -795,9 +793,3 @@ components:
description: The paid amount of the invoice.
format: double
example: 100.00
securitySchemes:
bearerAuth:
type: http
bearerFormat: JWT
scheme: bearer

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.2.1</version>
<executions>
<execution>
<execution>
<goals>
<goal>generate</goal>
</goals>
Expand Down
9 changes: 0 additions & 9 deletions src/co/icreated/portal/api/service/CommonApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ public interface CommonApi {
@ApiResponse(responseCode = "200", description = "OK", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ValueLabelDto.class))
})
},
security = {
@SecurityRequirement(name = "bearerAuth")
}
)
@RequestMapping(
Expand Down Expand Up @@ -84,9 +81,6 @@ ResponseEntity<List<ValueLabelDto>> getCreditCardTypes(
@ApiResponse(responseCode = "200", description = "Unexpected error", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = PortalErrorDto.class))
})
},
security = {
@SecurityRequirement(name = "bearerAuth")
}
)
@RequestMapping(
Expand Down Expand Up @@ -120,9 +114,6 @@ ResponseEntity<String> getDocStatus(
@ApiResponse(responseCode = "200", description = "Unexpected error", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = PortalErrorDto.class))
})
},
security = {
@SecurityRequirement(name = "bearerAuth")
}
)
@RequestMapping(
Expand Down
9 changes: 0 additions & 9 deletions src/co/icreated/portal/api/service/InvoicesApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ public interface InvoicesApi {
@ApiResponse(responseCode = "200", description = "Unexpected error", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = PortalErrorDto.class))
})
},
security = {
@SecurityRequirement(name = "bearerAuth")
}
)
@RequestMapping(
Expand Down Expand Up @@ -89,9 +86,6 @@ ResponseEntity<InvoiceDto> getInvoice(
@ApiResponse(responseCode = "200", description = "Unexpected error", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = PortalErrorDto.class))
})
},
security = {
@SecurityRequirement(name = "bearerAuth")
}
)
@RequestMapping(
Expand Down Expand Up @@ -122,9 +116,6 @@ ResponseEntity<List<DocumentDto>> getInvoices(
@ApiResponse(responseCode = "200", description = "Unexpected error", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = PortalErrorDto.class))
})
},
security = {
@SecurityRequirement(name = "bearerAuth")
}
)
@RequestMapping(
Expand Down
6 changes: 0 additions & 6 deletions src/co/icreated/portal/api/service/PaymentsApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public interface PaymentsApi {
@ApiResponse(responseCode = "200", description = "Unexpected error", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = PortalErrorDto.class))
})
},
security = {
@SecurityRequirement(name = "bearerAuth")
}
)
@RequestMapping(
Expand Down Expand Up @@ -83,9 +80,6 @@ ResponseEntity<Void> createPayment(
@ApiResponse(responseCode = "200", description = "OK", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = PaymentDto.class))
})
},
security = {
@SecurityRequirement(name = "bearerAuth")
}
)
@RequestMapping(
Expand Down
9 changes: 0 additions & 9 deletions src/co/icreated/portal/api/service/UsersApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ public interface UsersApi {
@ApiResponse(responseCode = "200", description = "Unexpected error", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = PortalErrorDto.class))
})
},
security = {
@SecurityRequirement(name = "bearerAuth")
}
)
@RequestMapping(
Expand Down Expand Up @@ -89,9 +86,6 @@ ResponseEntity<Void> sendEmailToken(
@ApiResponse(responseCode = "200", description = "Unexpected error", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = PortalErrorDto.class))
})
},
security = {
@SecurityRequirement(name = "bearerAuth")
}
)
@RequestMapping(
Expand Down Expand Up @@ -125,9 +119,6 @@ ResponseEntity<Void> updateForgottenPassword(
@ApiResponse(responseCode = "200", description = "Unexpected error", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = PortalErrorDto.class))
})
},
security = {
@SecurityRequirement(name = "bearerAuth")
}
)
@RequestMapping(
Expand Down
26 changes: 5 additions & 21 deletions src/co/icreated/portal/controller/PaymentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import co.icreated.portal.security.Authenticated;
import co.icreated.portal.service.InvoiceService;
import co.icreated.portal.service.PaymentService;
import co.icreated.portal.utils.Transaction;

@RestController
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
Expand Down Expand Up @@ -74,27 +75,10 @@ public ResponseEntity<Void> createPayment(@Valid CreditCardDto creditCardDto) {
throw new AdempiereException("Totals from backend & frontend not are equal");
}

// TODO It's a good idea to put this Idempiere Trx deal to AOP Spring Annotation @Around
// For instance it's a unique case, but later it's a "must be"
String trxName = Trx.createTrxName("portalPayments");
Trx trx = Trx.get(trxName, true);

try {
paymentService.createPayments(getSessionUser(), openItems, creditCardDto, trxName);
trx.commit();
log.log(Level.INFO, "Transaction Payments Completed");
return ResponseEntity.ok().build();
} catch (Exception e) {
log.log(Level.WARNING, "Not proceed. Transaction Payments Aborted");
trx.rollback();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
} finally {
trx.close();
trx = null;
}



Transaction.run(trxName -> {
paymentService.createPayments(getSessionUser(), openItems, creditCardDto, trxName);
});
return null;
}


Expand Down
32 changes: 7 additions & 25 deletions src/co/icreated/portal/service/InvoiceService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package co.icreated.portal.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.compiere.model.MInvoice;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.springframework.stereotype.Service;

Expand All @@ -19,6 +15,7 @@
import co.icreated.portal.exceptions.PortalNotFoundException;
import co.icreated.portal.mapper.InvoiceMapper;
import co.icreated.portal.utils.PQuery;
import co.icreated.portal.utils.QueryTool;

@Service
public class InvoiceService {
Expand Down Expand Up @@ -89,16 +86,10 @@ public List<OpenItemDto> findOpenItems(int C_BPartner_ID) {
+ "FROM RV_OpenItem WHERE AD_Client_ID = ? AND C_BPartner_ID = ? AND isSOTrx='Y' "
+ "ORDER BY dateInvoiced DESC";

ArrayList<OpenItemDto> list = new ArrayList<OpenItemDto>();
try (PreparedStatement pstmtActual = DB.prepareStatement(sql, null);) {
pstmtActual.setInt(1, Env.getAD_Client_ID(ctx));
pstmtActual.setInt(2, C_BPartner_ID);
ResultSet rs = pstmtActual.executeQuery();

while (rs.next()) {
list.add(
Map<Integer, Object> params = Map.of(1, Env.getAD_Client_ID(ctx), 2, C_BPartner_ID);
return QueryTool.nativeList(sql, params, rs -> {
//@formatter:off
new OpenItemDto()
return new OpenItemDto()
.invoiceId(rs.getInt("C_Invoice_ID"))
.orderId(rs.getInt("C_Order_ID"))
.bpartnerId(rs.getInt("C_BPartner_ID"))
Expand All @@ -116,17 +107,8 @@ public List<OpenItemDto> findOpenItems(int C_BPartner_ID) {
.totalLines(rs.getBigDecimal("totalLines"))
.grandTotal(rs.getBigDecimal("grandTotal"))
.paidAmt(rs.getBigDecimal("paidAmt"))
.openAmt(rs.getBigDecimal("openAmt"))
.openAmt(rs.getBigDecimal("openAmt"));
//@formatter:on
);
}



} catch (SQLException e) {
e.printStackTrace();
}

return list;
});
}
}
38 changes: 15 additions & 23 deletions src/co/icreated/portal/service/PaymentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public void createPayments(SessionUser sessionUser, List<OpenItemDto> openItems,

openItems.stream()
.forEach(openItem -> createCreditCardPayment(sessionUser, openItem, creditCard, trxName));

}


Expand Down Expand Up @@ -100,8 +99,9 @@ public boolean createCreditCardPayment(SessionUser sessionUser, OpenItemDto open
.setParameters(Env.getAD_Org_ID(ctx), openItem.getCurrencyId()) //
.setOrderBy("IsDefault DESC") //
.first();
if (ba != null)
if (ba != null) {
payment.setC_BankAccount_ID(ba.getC_BankAccount_ID());
}


MDocType[] doctypes = MDocType.getOfDocBaseType(ctx, MDocType.DOCBASETYPE_ARReceipt);
Expand All @@ -120,33 +120,25 @@ public boolean createCreditCardPayment(SessionUser sessionUser, OpenItemDto open


payment.processIt(DocAction.ACTION_Complete);
boolean ok = payment.save();
return ok;
return payment.save();
}



public MBPBankAccount getBankAccount(SessionUser sessionUser, OpenItemDto openItem,
String trxName) {

String trxName) {
MBPartner bp = new MBPartner(ctx, sessionUser.getPartnerId(), trxName);

Stream<MBPBankAccount> stream = Arrays.stream(bp.getBankAccounts(true));
MBPBankAccount retValue = stream //
.filter(item -> item.getAD_User_ID() == sessionUser.getUserId() && item.isActive()) //
.findFirst().orElse(null);

// create new
if (retValue == null) {
MUser user = new MUser(ctx, sessionUser.getUserId(), trxName);

MLocation location = MLocation.getBPLocation(ctx, openItem.getBpartnerLocationId(), trxName);
retValue = new MBPBankAccount(ctx, bp, user, location);
retValue.setAD_User_ID(sessionUser.getUserId());
retValue.save();
}

return retValue;
return Stream.of(bp.getBankAccounts(false))
.filter(ba -> ba.getAD_User_ID() == sessionUser.getUserId()).findAny()
.orElseGet(() -> {
MUser user = new MUser(ctx, sessionUser.getUserId(), trxName);
MLocation location = MLocation.get(ctx, openItem.getBpartnerLocationId(), trxName);
MBPBankAccount bankAccount = new MBPBankAccount(ctx, bp, user, location);
bankAccount.setAD_User_ID(user.getAD_User_ID());
bankAccount.save();
return bankAccount;
});
}

}
9 changes: 6 additions & 3 deletions src/co/icreated/portal/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package co.icreated.portal.service;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;
import java.util.Properties;

Expand Down Expand Up @@ -64,7 +65,8 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
*/
public SessionUser findSessionUserByValue(String value) {

MUser user = new PQuery(ctx, MUser.Table_Name, "Value LIKE ?", null).setParameters(value) //
MUser user = new PQuery(ctx, MUser.Table_Name, "Value LIKE ?", null) //
.setParameters(value) //
.first();

if (user == null) {
Expand Down Expand Up @@ -102,7 +104,8 @@ public MUser getUserByParam(Object value, String sql) {
throw new PortalInvalidInputException("User not defined");
}

MUser user = new PQuery(ctx, MUser.Table_Name, sql, null).setParameters(value) //
MUser user = new PQuery(ctx, MUser.Table_Name, sql, null) //
.setParameters(value) //
.first();

if (user == null) {
Expand All @@ -123,7 +126,7 @@ public boolean changePassword(String newPassword, MUser user) {

user.setPassword(newPassword);
user.setIsLocked(false);
user.setDatePasswordChanged(new Timestamp(System.currentTimeMillis()));
user.setDatePasswordChanged(Timestamp.from(Instant.now()));
user.setEMailVerifyCode(user.getEMailVerifyCode(), "By Changing password");
return user.save();
}
Expand Down
Loading

0 comments on commit b96662a

Please sign in to comment.