Overriding translations from admin-web in Liferay 7 - liferay

Thera are some tranlations in com.liferay.plugins.admin.web or com.liferay.portal.instances.web module which I'd like to override. With other modules I've followed succesfully this tutorial:
https://dev.liferay.com/develop/tutorials/-/knowledge_base/7-0/overriding-a-modules-language-keys
In this case, com.liferay.plugins.admin.web module has no servlet.context.name, which is required in class properties. Is there any way to override this tranlations? Thanks for help in advance!

The best solution is to create a translation module which extends from ResourceBundle:
package com.galian.extranet.resourcebundle;
import com.liferay.portal.kernel.language.UTF8Control;
import java.util.Enumeration;
import java.util.ResourceBundle;
import org.osgi.service.component.annotations.Component;
/**
* #author
*
*/
#Component(immediate = true, property = { "language.id=en_US" }, service = ResourceBundle.class)
public class DefaultCustomResourceBundle extends ResourceBundle {
#Override
public Enumeration<String> getKeys() {
return _resourceBundle.getKeys();
}
#Override
protected Object handleGetObject(String key) {
return _resourceBundle.getObject(key);
}
private final ResourceBundle _resourceBundle = ResourceBundle.getBundle("content.Language", UTF8Control.INSTANCE);
}
Your module project structure will be like this:

Related

Why import cucumber.api.junit.Cucumber; is not working after spring boot upgrades?

I upgrade spring boot version 2.2.0.RELEASE to 2.7.4. After that import cucumber.api.junit.Cucumber; is not supported. This is the code,
import cucumber.api.junit.Cucumber;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;
import net.masterthought.cucumber.json.support.Status;
import net.masterthought.cucumber.presentation.PresentationMode;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;
/**
* Class to generate html report from cucumber runner's json report output.
*/
public class CucumberReportRunner extends Cucumber {
private static final String PROJECT_NAME = "Hello Cucumber & Spring Boot";
private static final String BUILD_NUMBER = "1.0.0";
private static final String BRANCH_NAME = "master";
public CucumberReportRunner(Class clazz) throws InitializationError {
super(clazz);
}
#Override
public void run(RunNotifier notifier) {
super.run(notifier);
generateReport();
}
public static void generateReport() {
File reportOutputDirectory = new File("target/html");
List<String> jsonFiles = new ArrayList<>();
jsonFiles.add("target/cucumber-report.json");
Configuration configuration = new Configuration(reportOutputDirectory, PROJECT_NAME);
configuration.addPresentationModes(PresentationMode.RUN_WITH_JENKINS);
configuration.setNotFailingStatuses(Collections.singleton(Status.SKIPPED));
configuration.setBuildNumber(BUILD_NUMBER);
configuration.addClassifications("Build Number", configuration.getBuildNumber());
configuration.addClassifications("Branch Name", BRANCH_NAME);
ReportBuilder reportBuilder = new ReportBuilder(jsonFiles, configuration);
reportBuilder.generateReports();
}
}
In the code ,
extends Cucumber
import cucumber.api.junit.Cucumber;
#Override
public void run(RunNotifier notifier) {
super.run(notifier);
code lines are shown in error.
and terminal getting this error,
/workspase/test-project/src/test/java/com/textile/bdd/CucumberReportRunner.java:3: error: package cucumber.api.junit does not exist
import cucumber.api.junit.Cucumber;
^
/workspase/test-project/src/test/java/com/textile/bdd/CucumberReportRunner.java:18: error: cannot find symbol
public class CucumberReportRunner extends Cucumber {
^
symbol: class Cucumber
/workspase/test-project/src/test/java/com/textile/bdd/TestRunner.java:11: error: cannot find symbol
#RunWith(Cucumber.class)
^
symbol: class Cucumber
What would be the best possible options?

How to test a Controller and Model in a JSF Project with jUnit?

i don't know exactly how to write tests for these following Classes especially for the Controller and Model. Is it to possible to test with jUnit ?
I heard from Selenium but first i would test with jUnit. Thanks for ur help and best regards.
Controller.class:
import factory.InfoMessageFactory;
import entity.Product;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import model.ProductModel;
import project.Konstanten;
#Named(value = "ProductController")
#SessionScoped
public class ProductController implements Serializable {
private Product product;
#Inject
private ProductModel model;
#PostConstruct
public void init() {
this.product = new Product();
}
public String addProduct() {
this.model.newProduct(this.product);
}
public Product getProduct() {
return product;
}
public void setProdukt(Product product) {
this.product = product;
}
public List<Product> getProducts() {
return this.model.getProducts();
}
}
Model.class
package model;
import ejb.DB;
import entity.Product;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
#Dependent
public class ProductModel implements Serializable{
#Inject
private DB db;
public boolean addProduct(Product p){
try{
db.persist(p);
}catch(Exception e){
System.out.println("Blablabla");
return false;
}
return true;
}
}
And DB.class
#Stateless
public class DB {
#Inject
#RealClass
private EntityManager em;
public void persist(Object object) {
em.persist(object);
}
In the ProductController, there is really not much to test.. unless there is more logic that you did not post.
For testing the ProductModel, or any service-like class having the DB dependency i would suggest adding a project dependency to one of the mocking frameworks (i suggest Mockito as it is the most mature of them all).
For the addProducts method you could end up with following tests:
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
public class ProductModelTest{
#Mock
private DB dbMock;
#InjectMocks
private ProdcutModel = new ProductModel();
#Before
public void init(){
MockitoAnnotations.iniMocks(this);
}
#Test
public void shouldReturnTrue_whenEntityPersisted(){
doNothing().when(dbMock).persist(any(Product.class));
boolean result = productModel.addProduct(new Product());
assertTrue(result);
}
#Test
public void shouldReturnFalse_whenEntityPersisted(){
doThrow(RuntimeException.class).when(dbMock).persist(any(Product.class));
boolean result = productModel.addProduct(new Product());
assertFalse(result);
}
}
Regarding the DB-like repository classes.. i normally do not unit-test them. IF so i run integration tests on them.

FacesContextListener implementation

In an attempt to implement the FacesContextListener, which seems to be the ideal place for the current challenge we are facing, I still struggle with its implementation. Attempts to declare it in the faces-config.xml or a construction similar to the ApplicationListener failed (as I probably reference things wrong (except for the class itself of course)).
Can someone provide directions / a short example regarding the implementation of the FacesContextListener?
Create a Java class which implements FacesContextListener interface.
package ch.hasselba.xpages;
import javax.faces.context.FacesContext;
import com.ibm.xsp.event.FacesContextListener;
public class MyFacesContextListener implements FacesContextListener {
public void beforeContextReleased(FacesContext fc) {
System.out.println("beforeContextReleased");
}
public void beforeRenderingPhase(FacesContext fc) {
System.out.println("beforeRenderingPhase");
}
}
Now, add an instance of the class to your XPage:
importPackage( ch.hasselba.xpages )
var fcl = new ch.hasselba.xpages.MyFacesContextListener();
facesContext.addRequestListener( fcl );
Hope this helps!
EDIT:
Here is a Java implementation with an anonymous Listener:
package ch.hasselba.xpages;
import javax.faces.context.FacesContext;
import com.ibm.xsp.context.FacesContextExImpl;
import com.ibm.xsp.event.FacesContextListener;
public class MyObject {
private transient FacesContextListener mFCListener;
public MyObject() {
mFCListener = new FacesContextListener() {
public void beforeContextReleased(FacesContext fc) {
System.out.println("Before Releasing.");
}
public void beforeRenderingPhase(FacesContext fc) {
System.out.println("Before Rendering.");
}
};
FacesContextExImpl fc = (FacesContextExImpl) FacesContext.getCurrentInstance();
fc.addRequestListener( this.mFCListener );
}
}

CDI #Decorator, Is there a way to deactivate a decorator at runtime?

I use the following workaround in order to control the behaviour of a #Decorator since I couldn't find a way to deactivate it.
if (!FacesContext.getCurrentInstance().getViewRoot().getViewId()
.endsWith("decoratorDemo.xhtml")) {
return transInterBean.getTransactionalInsertRecords();
} else {
...
}
Is there no way to decide at runtime whether a decorator should be applied?
package com.cdi.decorators;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.enterprise.inject.Any;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import com.cdi.cdibeans.TransactionalInterceptor;
import com.cdi.cdibeans.TransactionalInterceptorBean;
#Decorator
public abstract class TransactionalInterceptorDecorator implements
TransactionalInterceptor {
/**
*
*/
private static final long serialVersionUID = -1191671082441891759L;
#Inject
#Delegate
#Any
TransactionalInterceptorBean transInterBean;
#Override
public ArrayList<String> getTransactionalInsertRecords()
throws SQLException {
ArrayList<String> records = new ArrayList<String>();
if (!FacesContext.getCurrentInstance().getViewRoot().getViewId()
.endsWith("decoratorDemo.xhtml")) {
return transInterBean.getTransactionalInsertRecords();
} else {
Iterator<String> iter = transInterBean
.getTransactionalInsertRecords().iterator();
while (iter.hasNext()) {
String record = iter.next();
records.add(record);
records.add(">>>Decorator<<< Added record ... ");
}
if (records.isEmpty()) {
records.add(">>>Decorator<<< Currently there are no records yet!");
}
return records;
}
}
}
Deltaspike has an exclude feature ... may be this could help, I didn't try it with decorators.

Adding a TilePane instantiated in .java files to FXML

I'm trying to add a TilePane with ImageView children to a scene in JavaFX. Currently, my FXML is loading an empty TilePane.
The current FXML line that i have making the TilePane is
<TilePane id="MapPane" fx:id="mapPane" layoutX="3.0" layoutY="0.0" prefColumns="9" prefHeight="560.0" prefTileHeight="112.0" prefTileWidth="112.0" prefWidth="1277.0" visible="true"\>
where mapPane is the name of the variable in my .java file
controller:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package screens.gameScreen;
import screens.*;
import mule.*;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.*;
import java.awt.MouseInfo;
import java.awt.Point;
import com.sun.glass.ui.Robot;
/**
* FXML Controller class
*
* #author Stephen
*/
public class GameScreenController implements Initializable, ControlledScreen {
Robot robot = com.sun.glass.ui.Application.GetApplication().createRobot();
ScreenManager screenManager;
TileEngine tileEngine = new TileEngine();
#FXML
TilePane mapPane = tileEngine.createRandomMap(true);;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
}
#Override
public void setScreenParent(ScreenManager screen) {
screenManager = screen;
}
#FXML
private void goToMain(ActionEvent event) {
screenManager.setScreen(mule.MULE.mainMenuScreenID);
}
}
you can use SceneBuilder for create FXML. This can easily solve your problem.

Resources