001/* 002 * Copyright 2012 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.core.right.profile; 017 018import java.io.IOException; 019import java.util.List; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.cocoon.ProcessingException; 024import org.apache.cocoon.environment.ObjectModelHelper; 025import org.apache.cocoon.environment.Request; 026import org.apache.cocoon.generation.ServiceableGenerator; 027import org.apache.cocoon.xml.AttributesImpl; 028import org.apache.cocoon.xml.XMLUtils; 029import org.apache.commons.lang3.StringUtils; 030import org.xml.sax.SAXException; 031 032import org.ametys.core.right.Profile; 033import org.ametys.core.right.Right; 034import org.ametys.core.right.RightProfilesDAO; 035import org.ametys.core.right.RightsExtensionPoint; 036 037/** 038 * Generates the rights of a profile 039 */ 040public class ProfileRightsGenerator extends ServiceableGenerator 041{ 042 private RightsExtensionPoint _rights; 043 private RightProfilesDAO _profilesDAO; 044 045 @Override 046 public void service(ServiceManager m) throws ServiceException 047 { 048 super.service(m); 049 _rights = (RightsExtensionPoint) m.lookup(RightsExtensionPoint.ROLE); 050 _profilesDAO = (RightProfilesDAO) m.lookup(RightProfilesDAO.ROLE); 051 } 052 053 @Override 054 public void generate() throws IOException, SAXException, ProcessingException 055 { 056 Request request = ObjectModelHelper.getRequest(objectModel); 057 String id = request.getParameter("id"); 058 059 contentHandler.startDocument(); 060 061 if (StringUtils.isEmpty(id)) 062 { 063 XMLUtils.startElement(contentHandler, "profiles"); 064 065 List<Profile> profiles = _profilesDAO.getProfiles(); 066 for (Profile profile : profiles) 067 { 068 _saxProfileRights(profile); 069 } 070 071 XMLUtils.endElement(contentHandler, "profiles"); 072 } 073 else 074 { 075 Profile profile = _profilesDAO.getProfile(id); 076 if (profile != null) 077 { 078 _saxProfileRights (profile); 079 } 080 else 081 { 082 XMLUtils.createElement(contentHandler, "profile"); 083 } 084 } 085 086 contentHandler.endDocument(); 087 } 088 089 private void _saxProfileRights (Profile profile) throws SAXException 090 { 091 AttributesImpl attr = new AttributesImpl(); 092 attr.addCDATAAttribute("id", profile.getId()); 093 XMLUtils.startElement(contentHandler, "profile", attr); 094 095 List<String> rights = _profilesDAO.getRights(profile); 096 for (String rightId : rights) 097 { 098 Right right = _rights.getExtension(rightId); 099 if (right != null) 100 { 101 right.toSAX(contentHandler); 102 } 103 } 104 105 XMLUtils.endElement(contentHandler, "profile"); 106 } 107}