Error in sessionMessages.add() method when migrated from liferay 6.2 to liferay DXP - liferay-7

This is my current code in liferay 6.2.
SessionMessages.add(actionRequest.getPortletSession(),
"User-Approved");
The sessionmessages.add() method no longer can take portlet session as arg.Then how i change the argument in liferay 7

Try replace your code with:
SessionMessages.add(actionRequest,"User-Approved");
because javax.portlet.ActionRequest extends javax.portlet.PortletRequest interface and there is a SessionMessages.add method with that parameter type:
public static void add(PortletRequest portletRequest, String key) {
Map<String, Object> map = _getMap(portletRequest, true);
if (map == null) {
return;
}
map.put(key, key);
}

You can still pass the whole request object.
public static void add(PortletRequest portletRequest, String key)

Related

Dbcontext disposed exception EF core 3.1

I recently migrated my web application from ASP.net to.NETCore, I have already registered the DBcontext using DI in my startup.cs
services.AddDbContext<MyContextDB>
(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")),
....
public partial class MyContextDB: IdentityDbContext<USER>, IMyContextDB
...
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseLazyLoadingProxies();
}
I have also avoided the use of "using" to retrieve data through the context , and I verified that I don't have any call to manually "Dispose()" the context
However I keep getting this exception whenever I reach this part of the application :
public class Licence : ILicence
{
private static IOptionsMonitor<LicenceConfiguration> _appSettings;
private readonly MyContextDB _context;
public Licence(MyContextDB context, IOptionsMonitor<LicenceConfiguration> optionsAccessor)
{
_context = context;
_appSettings = optionsAccessor;
}
public LICENCECODE GetLicenceCode(string key)
{
LICENCECODE LicenceCode = null;
LicenceCode = _context.LICENCECODE.SingleOrDefault(l => l.LICENCEKEY == key);
return LicenceCode;
}
}
"Cannot access a disposed object. A common cause of this error is
disposing a context that was resolved from dependency injection and
then later trying to use the same context instance elsewhere in your
application. This may occur if you are calling Dispose() on the
context, or wrapping the context in a using statement. If you are
using dependency injection, you should let the dependency injection
container take care of disposing context instances.\r\nObject name:
'MyContextDB'."
I've been through every article available on the internet about this exception but yet I can't identify the actual cause of it.
Could you please help me resolve it
The exception is raised specifically when this call is made :
public async Task<LICENCECODE> GetLicenceCode(string key)
{
LICENCECODE LicenceCode = null;
LicenceCode = await _context.LICENCECODE.SingleOrDefaultAsync(l => l.LICENCEKEY == key);
return LicenceCode;
}
PS: I tried to change the method to async because I thought that could be the cause of the issue but unfortunately it's still happening.
the call to that method is from another Model class
validLicence = _licence.CheckLicence(type.Name, ref message, out maxCount);
....
and then inside
CheckLicence
LICENCECODE LicenceCode = GetLicenceCode(LicenceKey).Result;
I guess it depends on how you are using your Licence class, but it might have to do with the lifetime of the request ending before you complete your work in Licence. This could for example happen if your controller action is asynchronous.
Try registering the class with transient lifetime instead:
services.AddTransient<ILicence, Licence>();

How to share data between a page and a component in AEM 6.2?

Is there a way how I can call the same instance of a model within HTL using the same data?
I want to create an object within the model of a page, let's say a String object, and then use it in the model of a component.
To create the bean (or model instance), I call
<sly data-sly-use.model="myModel"/>
in the page and in the component
Problem is that I have now 2 instances with 2 sets of local data - what I do NOT want to have.
The SlingHttpServletRequest (in general) provides an instance of SlingBindings, which contains a reference to "currentPage" (I am using the static field WCMBindings.CURRENT_PAGE [dependency: groupId: com.adobe.cq.sightly, artifactId: cq-wcm-sightly-extension, version: 1.2.30] in my example).
The Optional I am using in my example is a Java 8 class which can be used to avoid too many checks for null references.
final Optional<Page> optional = Optional.ofNullable(request)
.map(req -> (SlingBindings) req.getAttribute(SlingBindings.class.getName()))
.map(b -> (Page) b.get(WCMBindings.CURRENT_PAGE));
A simplified/explicit example would be
Page getCurrentPageFromRequest(#Nonnull final SlingHttpServletRequest request) {
final SlingBindings bindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName())
if (bindings == null) {
return null;
}
return (Page) bindings.get(WCMBindings.CURRENT_PAGE);
}
In your Sling model you would just call
#Model(adaptables={ SlingHttpServletRequest.class, })
public class Model {
public Model(#Nonnull final SlingHttpServletRequest request) {
final Page currentPage = getCurrentPageFromRequest(request);
// read properties.
}
Page getCurrentPageFromRequest(#Nonnull final SlingHttpServletRequest request) {
final SlingBindings bindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName())
if (bindings == null) {
return null;
}
return (Page) bindings.get(WCMBindings.CURRENT_PAGE);
}
}

What is the equivalent of IVisitor.CONTINUE TRAVERSAL in wicket 1.5

I'm porting our Wicket 1.4 app to Wicket 1.5. Visitors are now very different. What I would like to know is how do I handle a CONTINUAL_TRAVERSAL in Wicket 1.5? The existing 1.4 code is below:
public class MyFormVisitor implements IVisitor<Component, Object>, Serializable {
private static final long serialVersionUID = 7271477325583441433L;
private Set<Component> visited = new HashSet<Component>();
#Override
public Object component(Component c) {
if (!visited.contains(c)) {
visited.add(c);
c.add(new MandatoryBehavior());
c.add(new ErrorHighlightBehavior());
}
return IVisitor.CONTINUE_TRAVERSAL;
}
Just convert your method to something like this and you should be fine:
#Override
public void component(final Component c, final IVisit<Void> visit) {
if (!visited.contains(c)) {
visited.add(c);
c.add(new MandatoryBehavior());
c.add(new ErrorHighlightBehavior());
}
}
As you can see in the documentation you linked, the traversal is now controlled via the IVisit passed to the method. If none of the methods to either stop or not go deeper is called, the traversal will simply continue.

Place name without a colon (:)?

Still a bit of a GWT noob here but making progress using Activities and Places as described by Google here.
I understand that a Place's "URL consists of the Place's simple class name (like "HelloPlace") followed by a colon (:) and the token returned by the PlaceTokenizer.
Can I somehow remove the colon when I don't have a token to send?
For example, I'm fine with a URL like this "#editPerson:2" when I need to work with PersonId=2. But what about when I just want to present a blank Person form? In that case I would prefer to use "#addPersonForm" rather than "#addPersonForm:"
Any suggestions (even better code suggestions) would be most appreciated!
You can provide your own PlaceHistoryMapper (without using the generator) as already suggested by Boris_siroB, or you can do it within a PlaceTokenizer with an empty prefix: with an empty prefix, there won't be a colon, and the tokenizer can do whatever you want. If you totally distinct places, make it a tokenizer of Place, so it's also the "catchall" for getToken. That way you can keep all the advantages of the generation with prefixes, PlaceTokenizers and WithTokenizers (if you want to take advantage of them)
To take full control of the URL hash (that is to generate your own tokens from Places and map these tokens back to Places) you can implement your own history mapper (a class implementing the PlaceHistoryMapper interface).
public class MyPlaceHistoryMapper implements PlaceHistoryMapper {
#Override
public Place getPlace(String token) {
// parse tokens and create Places here
}
#Override
public String getToken(Place place) {
// examine Places and compose tokens here
}
}
In your entry point class you'd then replace the line:
AppPlaceHistoryMapper historyMapper = GWT.create(AppPlaceHistoryMapper.class);
with:
PlaceHistoryMapper appHistoryMapper = new MyPlaceHistoryMapper();
That's it. Your URL hashes no longer need to be class name-based or to use the : delimiter.
I'm using a PlaceHistoryMapper decorator named PlaceHistoryMapperWithoutColon.
Usage :
final PlaceHistoryMapper historyMapper0 = GWT
.create(PlaceHistoryMapperImpl.class);
final PlaceHistoryMapper historyMapper = new PlaceHistoryMapperWithoutColon(historyMapper0);
Decorator source :
public class PlaceHistoryMapperWithoutColon implements PlaceHistoryMapper {
private static final String COLON = ":";
private PlaceHistoryMapper placeHistoryMapper;
public PlaceHistoryMapperWithoutColon(PlaceHistoryMapper placeHistoryMapper) {
this.placeHistoryMapper = placeHistoryMapper;
}
#Override
public Place getPlace(String token) {
if (token != null && !token.endsWith(COLON)) {
token = token.concat(COLON);
}
return placeHistoryMapper.getPlace(token);
}
#Override
public String getToken(Place place) {
String token = placeHistoryMapper.getToken(place);
if (token != null && token.endsWith(COLON)) {
token = token.substring(0, token.length() - 1);
}
return token;
}
}
Decorated source example :
#WithTokenizers({ FirstPlace.Tokenizer.class, SecondPlace.Tokenizer.class })
public interface PlaceHistoryMapperImpl extends PlaceHistoryMapper {
}
Place source example :
public final class FirstPlace extends Place {
#Prefix("first")
public static class Tokenizer implements PlaceTokenizer<FirstPlace> {
#Override
public NetworkInfosPlace getPlace(String token) {
return new FirstPlace ();
}
#Override
public String getToken(FirstPlace place) {
return "";
}
}
}

How to redirect to an anchor in JSF?

Let's say I have this action in a JSF Managed Bean:
public String doSomething() {
FacesContext.getCurrentInstance().getExternalContext().getFlash().put("msg", "Something was done successfully");
return "view?faces-redirect=true";
}
My view has an anchor element with the id msg. I want the url to have this anchor (for accessibility matters), like:
view.jsf#msg
Or whatever is my FacesServlet filter pattern.
return "view#msg?faces-redirect=true"; obviously will not work because JSF (mojarra at least) will try to evaluate view#msg as a view.
So my question is how to make JSF redirect to a URL with #msg in the end.
because JSF (mojarra at least) will try to evaluate view#msg as a view
Oh, that's nasty. It's definitely worth an enhancement request at the JSF/Mojarra boys.
Your best bet is to send the redirect manually with help of ExternalContext#redirect().
public void doSomething() throws IOException {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.getFlash().put("msg", "Something was done successfully");
ec.redirect("view.xhtml#msg");
}
(assuming that FacesServlet is mapped on *.xhtml)
Alternatively, you could conditionally render a piece of JS which does that instead.
<ui:fragment rendered="#{not empty flash.msg}">
<script>window.location.hash = 'msg';</script>
</ui:fragment>
You try to build an illegal URL - the fragment (#) is always the last part of an URL.
return "view?faces-redirect=true#msg" would be the correct URL.
Unfortunately that fragment is stripped by the default NavigationHandler, at least in JSF 2.2.
While the two options of BalusC are working as well, I have a third option to offer. Wrap the NavigationHandler and ViewHandler with a small patch:
public class MyViewHandler extends ViewHandlerWrapper {
public static final String REDIRECT_FRAGMENT_ATTRIBUTE = MyViewHandler.class.getSimpleName() + ".redirect.fragment";
// ... Constructor and getter snipped ...
public String getRedirectURL(final FacesContext context, final String viewId, final Map<String, List<String>> parameters, final boolean includeViewParams) {
final String redirectURL = super.getRedirectURL(context, viewId, removeNulls(parameters), includeViewParams);
final Object fragment = context.getAttributes().get(REDIRECT_FRAGMENT_ATTRIBUTE);
return fragment == null ? redirectURL : redirectURL + fragment;
}
}
public class MyNavigationHandler extends ConfigurableNavigationHandlerWrapper {
// ... Constructor and getter snipped ...
public void handleNavigation(final FacesContext context, final String fromAction, final String outcome) {
super.handleNavigation(context, fromAction,
storeFragment(context, outcome));
}
public void handleNavigation(final FacesContext context, final String fromAction, final String outcome, final String toFlowDocumentId) {
super.handleNavigation(context, fromAction,
storeFragment(context, outcome), toFlowDocumentId);
}
private static String storeFragment(final FacesContext context, final String outcome) {
if (outcome != null) {
final int hash = outcome.lastIndexOf('#');
if (hash >= 0 && hash + 1 < outcome.length() && outcome.charAt(hash + 1) != '{') {
context.getAttributes().put(MyViewHandler.REDIRECT_FRAGMENT_ATTRIBUTE, outcome.substring(hash));
return outcome.substring(0, hash);
}
}
return outcome;
}
}
(I had to create the wrapper for the ViewHandler anyway, because of a fix for JAVASERVERFACES-3154)