ParameterXmlContentHandler { Element createParameterElement( final ParameterDefinitionEntry parameter, final ParameterContext parameterContext, final Object selections, final ParameterDependencyGraph dependencies, final boolean ignoreAttributes ) throws BeanException, ReportDataFactoryException { try { final Element parameterElement = document.createElement( "parameter" ); parameterElement.setAttribute( "name", parameter.getName() ); final Class<?> valueType = parameter.getValueType(); parameterElement.setAttribute( "type", valueType.getName() ); parameterElement .setAttribute( "is-mandatory", String.valueOf( parameter.isMandatory() ) ); final String[] namespaces = parameter.getParameterAttributeNamespaces(); if ( !ignoreAttributes ) { for ( final String namespace : namespaces ) { final String[] attributeNames = parameter.getParameterAttributeNames( namespace ); for ( final String attributeName : attributeNames ) { final String attributeValue = parameter.getTranslatedParameterAttribute( namespace, attributeName, parameterContext ); final Element attributeElement = document.createElement( "attribute" ); attributeElement.setAttribute( "namespace", namespace ); attributeElement.setAttribute( "name", attributeName ); attributeElement.setAttribute( "value", attributeValue ); parameterElement.appendChild( attributeElement ); } } boolean shouldValidateOnServer = shouldAlwaysValidateOnServer( parameter, parameterContext ); final Set<String> dependentParams = dependencies.getDependentParameterFor( parameter.getName() ); if ( !dependentParams.isEmpty() ) { final Element oneMoreAttribute = document.createElement( "attribute" ); oneMoreAttribute.setAttribute( "namespace", SYS_SERVER_NAMESPACE ); oneMoreAttribute.setAttribute( "name", "has-downstream-dependent-parameter" ); oneMoreAttribute.setAttribute( "value", Boolean.TRUE.toString() ); parameterElement.appendChild( oneMoreAttribute ); shouldValidateOnServer = true; } if ( shouldValidateOnServer ) { final Element attrElement = document.createElement( "attribute" ); attrElement.setAttribute( "namespace", SYS_SERVER_NAMESPACE ); attrElement.setAttribute( "name", "must-validate-on-server" ); attrElement.setAttribute( "value", Boolean.TRUE.toString() ); parameterElement.appendChild( attrElement ); } } final Class<?> elementValueType; if ( valueType.isArray() ) { elementValueType = valueType.getComponentType(); } else { elementValueType = valueType; } final LinkedHashSet<Object> selectionSet = computeSelectionSet( parameter, parameterContext, selections, valueType ); if ( Date.class.isAssignableFrom( elementValueType ) ) { parameterElement .setAttribute( "timezone-hint", computeTimeZoneHint( parameter, parameterContext, selectionSet ) ); } @SuppressWarnings( "rawtypes" ) final LinkedHashSet handledValues = (LinkedHashSet) selectionSet.clone(); if ( parameter instanceof ListParameter ) { final ListParameter asListParam = (ListParameter) parameter; parameterElement.setAttribute( "is-multi-select", String.valueOf( asListParam.isAllowMultiSelection() ) ); parameterElement .setAttribute( "is-strict", String.valueOf( asListParam.isStrictValueCheck() ) ); parameterElement.setAttribute( "is-list", "true" ); final Element valuesElement = document.createElement( "values" ); parameterElement.appendChild( valuesElement ); final ParameterValues possibleValues = asListParam.getValues( parameterContext ); for ( int i = 0; i < possibleValues.getRowCount(); i++ ) { Object key = possibleValues.getKeyValue( i ); Object value = possibleValues.getTextValue( i ); final Element valueElement = document.createElement( "value" ); valuesElement.appendChild( valueElement ); if ( hasISOControlChars( key, elementValueType ) || hasISOControlChars( value, elementValueType ) ) { key = Base64.encodeBase64String( key.toString().getBytes() ); value = Base64.encodeBase64String( value.toString().getBytes() ); valueElement.setAttribute( "encoded", "true" ); } valueElement .setAttribute( "label", HtmlUtils.htmlEscape( String.valueOf( value ) ) ); valueElement.setAttribute( "type", elementValueType.getName() ); if ( key instanceof Number ) { final BigDecimal bd = new BigDecimal( String.valueOf( key ) ); valueElement.setAttribute( "selected", String.valueOf( selectionSet.contains( bd ) ) ); handledValues.remove( bd ); } else if ( key instanceof Timestamp ) { final Timestamp origKey = (Timestamp) possibleValues.getKeyValue( i ); valueElement.setAttribute( "selected", String.valueOf( selectionSet.contains( new Date( origKey.getTime() ) ) ) ); handledValues.remove( key ); } else if ( key == null ) { if ( ( selections == null ) || selectionSet.contains( null ) ) { valueElement.setAttribute( "selected", "true" ); handledValues.remove( null ); } } else { final Object origKey = possibleValues.getKeyValue( i ); valueElement.setAttribute( "selected", String.valueOf( selectionSet.contains( origKey ) ) ); handledValues.remove( key ); } if ( key == null ) { valueElement.setAttribute( "null", "true" ); } else { valueElement.setAttribute( "null", "false" ); valueElement.setAttribute( "value", convertParameterValueToString( parameter, parameterContext, key, elementValueType ) ); } } if ( !asListParam.isStrictValueCheck() ) { for ( final Object key : handledValues ) { final Element valueElement = document.createElement( "value" ); valuesElement.appendChild( valueElement ); valueElement.setAttribute( "label", Messages.getInstance() .getString( "ReportPlugin.autoParameter", String.valueOf( key ) ) ); valueElement.setAttribute( "type", elementValueType.getName() ); if ( key instanceof Number ) { final BigDecimal bd = new BigDecimal( String.valueOf( key ) ); valueElement.setAttribute( "selected", String.valueOf( selectionSet.contains( bd ) ) ); } else { valueElement.setAttribute( "selected", String.valueOf( selectionSet.contains( key ) ) ); } if ( key == null ) { valueElement.setAttribute( "null", "true" ); } else { valueElement.setAttribute( "null", "false" ); valueElement.setAttribute( "value", convertParameterValueToString( parameter, parameterContext, key, elementValueType ) ); } } } } else if ( parameter instanceof PlainParameter ) { parameterElement.setAttribute( "is-multi-select", "false" ); parameterElement.setAttribute( "is-strict", "false" ); parameterElement.setAttribute( "is-list", "false" ); if ( selections != null ) { final Element valuesElement = document.createElement( "values" ); parameterElement.appendChild( valuesElement ); final Element valueElement = document.createElement( "value" ); valuesElement.appendChild( valueElement ); valueElement.setAttribute( "type", valueType.getName() ); valueElement.setAttribute( "selected", "true" ); valueElement.setAttribute( "null", "false" ); final String value = convertParameterValueToString( parameter, parameterContext, selections, valueType ); valueElement.setAttribute( "value", value ); valueElement.setAttribute( "label", HtmlUtils.htmlEscape( value ) ); } } return parameterElement; } catch ( final BeanException be ) { logger.error( Messages.getInstance().getString( "ReportPlugin.errorFailedToGenerateParameter", parameter.getName(), String.valueOf( selections ) ), be ); throw be; } } ParameterXmlContentHandler( final ParameterContentGenerator contentGenerator, final boolean paginate ); static String convertParameterValueToString( final ParameterDefinitionEntry parameter,
                                                      final ParameterContext context, final Object value,
                                                      final Class<?> type ); void createParameterContent( final OutputStream outputStream,
                                      final Serializable fileId,
                                      final String path,
                                      boolean overrideOutputType,
                                      final MasterReport sourceReport ); static final String SYS_PARAM_ACCEPTED_PAGE; static final String SYS_PARAM_RENDER_MODE; static final String SYS_PARAM_CONTENT_LINK; static final String SYS_PARAM_SESSION_ID; static final String SYS_SERVER_NAMESPACE; }
@Test public void createParameterElementTest() throws BeanException, ReportDataFactoryException, ParserConfigurationException, XPathExpressionException { final DefaultListParameter parameter = new DefaultListParameter( "query", "c1", "c2", "name", true, true, String.class ); final ParameterContext context = getTestParameterContext(); handler.document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); final ParameterDependencyGraph dependencies = new ParameterDependencyGraph( "name", "dep1", "dep2" ); dependencies.addDependency( "name", "dep1" ); dependencies.addDependency( "name", "dep2" ); final Element element = handler.createParameterElement( parameter, context, null, dependencies, false ); assertNotNull( element ); handler.document.appendChild( element ); handler.createParameterDependencies( element, parameter, dependencies ); final String xml = toString( handler.document ); examineStandardXml( handler.document ); assertTrue( isThereAttributes( handler.document ) ); }
@Test public void createParameterElementNoAttributesTest() throws BeanException, ReportDataFactoryException, ParserConfigurationException, XPathExpressionException { final DefaultListParameter parameter = new DefaultListParameter( "query", "c1", "c2", "name", true, true, String.class ); final ParameterContext context = getTestParameterContext(); handler.document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); final ParameterDependencyGraph dependencies = new ParameterDependencyGraph( "name", "dep1", "dep2" ); dependencies.addDependency( "name", "dep1" ); dependencies.addDependency( "name", "dep2" ); final Element element = handler.createParameterElement( parameter, context, null, dependencies, Boolean.TRUE ); assertNotNull( element ); handler.document.appendChild( element ); examineStandardXml( handler.document ); assertFalse( isThereAttributes( handler.document ) ); }