Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You 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 jpaworkshop.presentation; import java.io.Serializable; import java.util.LinkedList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.enterprise.context.SessionScoped; import javax.inject.Inject; import javax.inject.Named; import jpaworkshop.application.CompanyService; import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; @Named @SessionScoped public class CompanyBean implements Serializable { private static final long serialVersionUID = 1L; private static final Logger logger = Logger.getLogger(CompanyBean.class.getName()); @Inject private CompanyService companyService; private List<String> results; private String query; public CompanyBean() { } public List<String> getResults() { return results; } public String getQuery() { return query; } public void setQuery(String query) { this.query = query; } public String executeQuery() { results = new LinkedList<String>(); try { List resultset = companyService.executeQuery(query); for (Object e : resultset) { results.add(convertResult(e)); } } catch (Exception ex) { logger.log(Level.WARNING, "exception while executing query: {0} {1}", new Object[] { query, ex }); results.add("error while executing query: " + ex.getMessage()); } return "ok"; } private String convertResult(Object e) { if (e == null) { return "null"; } else if (e instanceof Object[]) { Object[] row = (Object[]) e; StringBuilder builder = new StringBuilder(); for (Object col : row) { builder.append(convertResult(col)).append(","); } return builder.toString(); } else { return ReflectionToStringBuilder.toString(e, ToStringStyle.SHORT_PREFIX_STYLE); } } }