org.kisoonlineapp.uc.report.CalBuilder.java Source code

Java tutorial

Introduction

Here is the source code for org.kisoonlineapp.uc.report.CalBuilder.java

Source

/*
 * Copyright 2015 berni.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kisoonlineapp.uc.report;

import java.util.Calendar;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.TimeZone;
import net.fortuna.ical4j.model.TimeZoneRegistry;
import net.fortuna.ical4j.model.TimeZoneRegistryFactory;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.Description;
import net.fortuna.ical4j.model.property.Location;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Version;
import net.fortuna.ical4j.util.HostInfo;
import net.fortuna.ical4j.util.UidGenerator;
import org.apache.commons.lang3.StringUtils;
import org.kisoonlineapp.model.Veranstaltung;
import org.kisoonlineapp.uc.view.AdresseView;
import org.kisoonlineapp.uc.view.BeschreibungView;
import org.kisoonlineapp.uc.view.VeranstaltungView;

/**
 *
 * @author berni
 */
class CalBuilder {

    private static final Logger logger = Logger.getLogger(CalBuilder.class.getName());

    @Inject
    VeranstaltungView veranstaltungView;
    @Inject
    BeschreibungView beschreibungView;
    @Inject
    AdresseView adresseView;

    private HostInfo hostInfo;

    CalBuilder() {
        hostInfo = new HostInfo() {

            @Override
            public String getHostName() {
                return "some-host-name";
            }
        };
    }

    public net.fortuna.ical4j.model.Calendar generateCalendarFor(List<Veranstaltung> veranstaltungen) {
        Objects.requireNonNull(veranstaltungen);

        net.fortuna.ical4j.model.Calendar cal = new net.fortuna.ical4j.model.Calendar();
        cal.getProperties().add(new ProdId("-//Ben Fortuna//iCal4j 1.0.6//EN"));
        cal.getProperties().add(Version.VERSION_2_0);
        cal.getProperties().add(CalScale.GREGORIAN);
        for (Veranstaltung veranstaltung : veranstaltungen) {
            //---
            VEvent veranstaltungEvent = generateVEvntFor(veranstaltung);
            if (veranstaltungEvent != null) {
                cal.getComponents().add(veranstaltungEvent);
            }
        }
        return cal;
    }

    public VEvent generateVEvntFor(Veranstaltung veranstaltung) {
        Objects.requireNonNull(veranstaltung);

        VEvent veranstaltungEvent = null;
        //---
        try {
            final Calendar calBegin = veranstaltung.getBeginnEnde().calcBeginn();
            final Calendar calEnd = veranstaltung.getBeginnEnde().calcEnde();
            final String summary = veranstaltungView.formatVeranstaltung(veranstaltung);

            boolean guardGenerating = calBegin != null && calEnd != null;
            guardGenerating = guardGenerating && StringUtils.isNotBlank(summary);
            if (guardGenerating) {

                final TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
                final TimeZone tz = registry.getTimeZone("Europe/Vienna");

                final DateTime dt1 = new DateTime(calBegin.getTime());
                dt1.setTimeZone(tz);

                final DateTime dt2 = new DateTime(calEnd.getTime());
                dt2.setTimeZone(tz);

                veranstaltungEvent = new VEvent(dt1, dt2, summary);

                if (veranstaltung.getTreffpunktAdresse() != null) {
                    String aValue = adresseView
                            .formatBeschreibungUndStrasseNummerPlzOrt(veranstaltung.getTreffpunktAdresse());
                    Location location = new Location(aValue);
                    veranstaltungEvent.getProperties().add(location);
                }
                {
                    // Generate a UID for the event..
                    String pid = "" + Objects.hashCode(veranstaltung);
                    UidGenerator ug = new UidGenerator(hostInfo, pid);
                    veranstaltungEvent.getProperties().add(ug.generateUid());
                }
                if (veranstaltung.getBeschreibung() != null) {
                    String veranstaltungDescription = beschreibungView
                            .formatBeschreibungKurz(veranstaltung.getBeschreibung());
                    if (StringUtils.isNotBlank(veranstaltungDescription)) {
                        Description description = new Description(veranstaltungDescription);
                        veranstaltungEvent.getProperties().add(description);
                    }
                }
            }
        } catch (Exception ex) {
            logger.log(Level.WARNING, "Cannot create VEvent: " + veranstaltung, ex);
            veranstaltungEvent = null;
        }
        return veranstaltungEvent;
    }

}