-2

I have a bean named sampleBean scoped by viewScope .
this bean load some data from database (MySQL) .
my problem is some records shared between users .
now maybe [USER A] delete that shared record and i want to update view of other users .

I can not change scope to ApplicationScope because all records shared to all users .

How can fix this problem ?
Note : I read this post but can not understand how can fix this problem .
Note : I use Liberty 18.0.0.4 by JavaEE 8 (webProfile)

mah454
  • 1,571
  • 15
  • 38
  • 1
    Changing the scope to `@ApplicationScoped` would not solve anything. As the post you are linking to discusses, you can use a `f:websocket` to accomplish your above requirement. This tag requires JSF 2.3. To get it working with Liberty, please refer to https://www.ibm.com/support/knowledgecenter/en/SSEQTP_liberty/com.ibm.websphere.wlp.doc/ae/twlp_config_jsf23.html. Also, if you could provide us with a [mcve] that covers your case (maybe with a backing bean just providing random data), we could provide you with an example in your specific case. – Adam Waldenberg Jan 19 '19 at 07:09
  • I fixed problem , bad used cased "@Observer" in JSF . – mah454 Jan 19 '19 at 08:10
  • 1
    @mah454: This question is in its current state useless for others since it is not clear what the code (in [mcve] flavour) was and what the change should be.Can you improve it and create a good corresponding answer? – Kukeltje Jan 19 '19 at 10:07
  • @Kukeltje , I answered to my question , please vote up :D – mah454 Jan 20 '19 at 12:47
  • Sorry, no, voted down. See the comment. Answer is not the right way to do it (sort of wrong even) and there is no corresponding code in the question so it is hard for others to see what was wrong – Kukeltje Jan 21 '19 at 07:53

1 Answers1

-1

I fixed problem by this simple code . (I shared this code for you)

public class Information {
    private String name ;
    private String family ; 

    // constructor 
    // Getter & Setter 
    // override equal and hashCode

}

This is a simple service . (I simulated database on this class)

@Stateless
public class InformationService {

    private static final List<Information> db = new ArrayList<>();

    @Inject
    @Push(channel = "infoChannel")
    PushContext push;

    @PostConstruct
    public void init() {
        Information userA = new Information("John", "Vankate");
        Information userB = new Information("Julius", "Sampao");
        db.add(userA);
        db.add(userB);
    }

    public void remove(Information info) {
        db.remove(info);
        push.send("deleteInfo");
    }

    public List<Information> findAll() {
        return db;
    }
}

and simple JaxRs resources :

@Path("/info")
@RequestScoped
public class InformationResources {

    @EJB
    private InformationService informationService;


    @Path("/delete")
    @POST
    @Consumes("application/json")
    public String send(Information information) {
        informationService.remove(information);
        return "Receive : " + information;
    }
} 

Now Start JSF :

@Named
@ViewScoped
public class InformationBean implements Serializable {

    private Information info ;
    private List<Information> informationList ;

    @EJB
    private InformationService informationService ;


    @PostConstruct
    public void init() {
        informationList = informationService.findAll();
        info = new Information() ;
    }

    public void deleteInformation() {
        informationService.remove(info);
    }

    public Information getInfo() {
        return info;
    }

    public void setInfo(Information info) {
        this.info = info;
    }

    public List<Information> getInformationList() {
        return informationList;
    }

    public void setInformationList(List<Information> informationList) {
        this.informationList = informationList;
    }
}

and xhtml :

<h:body>
    <p:dataTable value="#{informationBean.informationList}" var="info" id="infoTable">
        <p:column rowHeader="name">
            <h:outputText value="#{info.name}"/>
        </p:column>
        <p:column rowHeader="family">
            <h:outputText value="#{info.family}"/>
        </p:column>
        <p:column rowHeader="action">
            <h:form>
                <p:commandButton value="Delete" action="#{informationBean.deleteInformation}">
                    <f:setPropertyActionListener value="#{info}" target="#{informationBean.info}"/>
                </p:commandButton>
            </h:form>
        </p:column>
    </p:dataTable>
    <hr/>
    <f:websocket channel="infoChannel">
        <p:ajax event="deleteInfo" update="infoTable"/>
    </f:websocket>
</h:body>

I already thought , PushContext must implemented on JSF beans , Now I understand can implement that in service or business logic layer .
Now you can remove information from JaxRs (Rest API) and record removed from p:dataTable without refresh page .
Note : this example use @ViewScoped

mah454
  • 1,571
  • 15
  • 38
  • It is bad practice to add front-end technology (Push) in backend services. The link you posted contains the info to do it right and it works (I use that all the time) And since you did not post what you tried before we cannot help and effectively this Q/A is sort of useless – Kukeltje Jan 21 '19 at 07:53
  • How do you update view if request received from webservice (Rest or soap)? – mah454 Jan 21 '19 at 19:53
  • 1
    The response should come from **A** service. The implementation of tbat service should be hidden from calling or depending services. For callback to the caller use an observer ;ike posted in the link – Kukeltje Jan 21 '19 at 21:59
  • @Kukeltje "@Observer" not working with viewScoped bean . How can fix this problem ? – mah454 Jan 24 '19 at 09:19
  • How is it not working with a viewscoped bean? And please make an [mcve] and do you actually need the 'push' related bean to be a viewsoped bean? – Kukeltje Jan 24 '19 at 10:07