no.dusken.aranea.admin.listener.PageChangeSectionListener.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.aranea.admin.listener.PageChangeSectionListener.java

Source

/*
 Copyright 2006 - 2011 Under Dusken
    
 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 no.dusken.aranea.admin.listener;

import no.dusken.aranea.model.Page;
import no.dusken.aranea.model.Section;
import no.dusken.aranea.model.SectionPage;
import no.dusken.aranea.service.PageService;
import no.dusken.aranea.service.SectionPageService;
import no.dusken.common.event.PreSaveEvent;
import org.springframework.context.ApplicationListener;

import javax.inject.Inject;

/**
 * @author Marvin B. Lillehaug <lillehau@underdusken.no>
 */
public class PageChangeSectionListener implements ApplicationListener<PreSaveEvent> {

    @Inject
    private SectionPageService sectionPageService;
    @Inject
    private PageService pageService;

    @Override
    public void onApplicationEvent(PreSaveEvent event) {
        if (event.getSource() instanceof Page) {
            Page source = (Page) event.getSource();

            if (!source.isNew()) {
                Page persistedPage = pageService.getEntity(source.getID());
                Section currentSection = source.getParent();
                Section originalSection = persistedPage.getParent();
                boolean sectionHasChanged = !originalSection.equals(currentSection);
                if (sectionHasChanged) {
                    SectionPage sectionPage = sectionPageService.getSectionPageBySectionAndPage(originalSection,
                            persistedPage);
                    if (sectionPage != null) {
                        sectionPage.setSection(currentSection);
                        sectionPageService.saveOrUpdate(sectionPage);
                    }
                }
            }
        }
    }

    public void setSectionPageService(SectionPageService sectionPageService) {
        this.sectionPageService = sectionPageService;
    }

    public void setPageService(PageService pageService) {
        this.pageService = pageService;
    }
}