/*
 * Copyright 2009 Kantega AS
 *
 * 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.kantega.publishing.admin.content.ajax;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import no.kantega.commons.log.Log;
import no.kantega.commons.client.util.RequestParameters;
import no.kantega.commons.exception.NotAuthorizedException;
import no.kantega.publishing.common.data.ContentIdentifier;
import no.kantega.publishing.common.service.ContentManagementService;
import no.kantega.publishing.common.Aksess;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * Update display period (publish and expire date) for a page
 */
public class UpdateDisplayPeriodAction implements Controller {
    private View aksessJsonView;

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> model = new HashMap<String, Object>();

        RequestParameters param = new RequestParameters(request);
        ContentManagementService cms = new ContentManagementService(request);

        try {
            int associationId = param.getInt("associationId");
            if (associationId != -1) {
                ContentIdentifier cid = new ContentIdentifier();
                cid.setAssociationId(associationId);

                Date publishDate = param.getDateAndTime("from", Aksess.getDefaultDateFormat());
                Date expireDate = param.getDateAndTime("end", Aksess.getDefaultDateFormat());

                boolean updateChildren = param.getBoolean("updateChildren", false);

                if (publishDate != null) {
                    cms.updateDisplayPeriodForContent(cid, publishDate, expireDate, updateChildren);
                }
            }

        } catch (NotAuthorizedException e) {
            Log.error(this.getClass().getName(), e, null, null);
            model.put("error", Boolean.TRUE);
        }

        return new ModelAndView(aksessJsonView, model);
    }

    public void setAksessJsonView(View aksessJsonView) {
        this.aksessJsonView = aksessJsonView;
    }
}

@@ -639,16 +639,125 @@
 .ajax;%0A%0A
+import no.kantega.publishing.admin.AdminSessionAttributes;%0Aimport no.kantega.publishing.common.data.Content;%0A
 import o
@@ -797,24 +797,24 @@
 delAndView;%0A
-
 import org.s
@@ -1384,16 +1384,55 @@
 equest;%0A
+import javax.servlet.http.HttpSession;%0A
 import j
@@ -2540,24 +2540,24 @@
  != null) %7B%0A
-
             
@@ -2644,16 +2644,928 @@
 ildren);
+%0A%0A                    // Update content objects stored in session so user is not confused%0A                    HttpSession session = request.getSession();%0A                    Content editedContent = (Content)session.getAttribute(AdminSessionAttributes.CURRENT_EDIT_CONTENT);%0A                    if (editedContent != null && editedContent.getId() == cid.getContentId()) %7B%0A                        editedContent.setPublishDate(publishDate);%0A                        editedContent.setExpireDate(expireDate);%0A                    %7D%0A                    Content displayedContent = (Content)session.getAttribute(AdminSessionAttributes.CURRENT_NAVIGATE_CONTENT);%0A                    if (displayedContent != null && displayedContent.getId() == cid.getContentId()) %7B%0A                        displayedContent.setPublishDate(publishDate);%0A                        displayedContent.setExpireDate(expireDate);%0A                    %7D
 %0A       

Update display period for pages stored in session