0

The thing is that I am using Hibernate on the server side and that I am sending basically "raw" database data to the client - which is fine I guess but that also means that my client gets a List<UpcomingEventDTO> when calling the according service which is just a list from a specified date to another one.

If I now want to split those events into a map where the keys map to lists of events of one day e.g. a Map<Integer, List<UpcomingEventDTO>> then I will have to do this on the client side. This wouldn't bother me if I wouldn't have to do that in my Presenter.

On the one hand I'm having the loading in my presenter:

private void loadUpcomingEvents(final Integer calendarWeekOffset) {

    new XsrfRequest<StoreServletAsync, List<UpcomingEventDTO>>(this.storeServlet) {

        @Override
        protected void onCall(AsyncCallback<List<UpcomingEventDTO>> asyncCallback) {
            storeServlet.getUpcomingEventsForCalendarWeek(storeId, calendarWeekOffset, asyncCallback);
        }

        @Override
        protected void onFailure(Throwable caught) {
        }

        @Override
        protected void onSuccess(List<UpcomingEventDTO> result) {
            upcomingEvents = result;
            presentUpcomingEvents();
        }

    }.request();
}

and the conversion of the data before I can present it:

private void presentUpcomingEvents() {

    Map<Integer, List<UpcomingEventDTO>> dayToUpcomingEvent = new HashMap<>();

    for (UpcomingEventDTO upcomingEvent : this.upcomingEvents) {

        @SuppressWarnings("deprecation")
        Integer day = upcomingEvent.getDate().getDay();
        List<UpcomingEventDTO> upcomingEvents = dayToUpcomingEvent.get(day);

        if(upcomingEvents == null) {
            upcomingEvents = new ArrayList<>();
        }

        upcomingEvents.add(upcomingEvent);
        dayToUpcomingEvent.put(day, upcomingEvents);
    }

    List<Integer> days = new ArrayList<Integer>(dayToUpcomingEvent.keySet());       
    Collections.sort(days);

    this.calendarWeekView.removeUpcomingEvent();

    for(Integer day : days) {
        CalendarDayPresenterImpl eventCalendarDayPresenter = null;
        eventCalendarDayPresenter = this.dayToEventCalendarDayPresenter.get(day);

        if(eventCalendarDayPresenter == null) {
            List<UpcomingEventDTO> upcomingEvents = dayToUpcomingEvent.get(day);
            eventCalendarDayPresenter = new CalendarDayPresenterImpl(upcomingEvents);
            this.dayToEventCalendarDayPresenter.put(day, eventCalendarDayPresenter);
        }

        this.calendarWeekView.appendEventCalendarDay(eventCalendarDayPresenter.getView());
    }
}

So my problem is basically that I am not really happy with having code like this in my presenter but on the other hand I wouldn't know how and where to provide the data in this "upgraded" form for my presenter(s).

One could argue and say that I could also just return the data from the server in a way I would need it on the server but then I would lose generality and I don't want to write for all views and presenters their "own" API to the database.

Another possibility would be e.g. to introduce another layer between the service/servlet layer and have something like a DAO- or database-layer before my presenters model. But this would also raise quite a lot questions for me. E.g. what would be the name of such a layer ^^ and would that layer provide "customize" data for presenters or would the data still be kind of generalized?

I'm having quite a huge issue figuring out what to do here so I hope I can benefit from someones experience.

Thanks a lot for any help here!

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • When you are trying to abstract some parts of the application, ask yourself a question - would that layer be used or reused anywhere else? I mean will it pay you later if you make it look nicer introducing this new layer? Will the app become more effective? Would your team members become more effective while reading/editing such optimized code? There are various opinions in my team on what should the data returned from the server look like (raw or prepared). While we keep agreeing on majority of the cases we use different approaches. Count also backend/frontend performance and bandwith use. – alexp Aug 27 '15 at 19:03

1 Answers1

-1

The presentation logic should be on server side in controller layer where its meant to prepare the view for the clients. ( MVC pattern ) And if many views want to use this, you can make an abstract controller which can be reused for other views.

Also its good to prepare your controller layer for the future requirements. Ask yourself whether another client will ask to present the data in different granularity ? May be show the upcoming events by month/time ? Hence you have to provide your API a granularity enum UPCOMING_EVENTS_DAY_GRANULARITY( DAY, MONTH, HOUR) as a method parameter so that you will make client to decide what they want.

And to make it more beautiful, you can also say rename/move controller layer into a webservice layer which can be considered as your future API for external systems (not only for your views but for anyone outside your system)..

Anudeep Gade
  • 1,365
  • 1
  • 9
  • 18