/** * Returns SearchResults for a given SolrDocumentList. A full results object with an "items" * wrapper element around the mods items is used to logically separate pagination, items and * facets in the XML * * @param doc solr document list to build results * @return the SearchResults object for this solr result * @see SearchResults */ private SearchResults buildFullResults(SolrDocumentList docs) { SearchResults results = new SearchResults(); Pagination pagination = new Pagination(); pagination.setNumFound(docs.getNumFound()); pagination.setStart(docs.getStart()); pagination.setRows(limit); // List<ModsType> modsTypes = new ArrayList<ModsType>(); ItemGroup itemGroup = new ItemGroup(); List<Item> items = new ArrayList<Item>(); for (final SolrDocument doc : docs) { Item item = new Item(); ModsType modsType = null; try { modsType = (new ItemDAO()).getModsType(doc); } catch (JAXBException je) { log.error(je.getMessage()); je.printStackTrace(); } // modsTypes.add(modsType); // items.add(item); item.setModsType(modsType); items.add(item); } // items.setModsType(modsType); itemGroup.setItems(items); results.setItemGroup(itemGroup); results.setPagination(pagination); if (facet != null) results.setFacet(facet); return results; }
public void addItemGroup(ItemGroup itemGroup) { SelectElement select = getSelectElement(); OptGroupElement optGroup = createOptGroupElement(itemGroup.getItem(), itemGroup.getClassName()); for (Item item : itemGroup.getItems()) { optGroup.appendChild(createOption(item)); } select.appendChild(optGroup); }
/** * Creates a {@link TopLevelItem} from the submission of the '/lib/hudson/newFromList/formList' or * throws an exception if it fails. */ public synchronized TopLevelItem createTopLevelItem(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException { acl.checkPermission(Job.CREATE); TopLevelItem result; String requestContentType = req.getContentType(); if (requestContentType == null) throw new Failure("No Content-Type header set"); boolean isXmlSubmission = requestContentType.startsWith("application/xml") || requestContentType.startsWith("text/xml"); String name = req.getParameter("name"); if (name == null) throw new Failure("Query parameter 'name' is required"); { // check if the name looks good Jenkins.checkGoodName(name); name = name.trim(); if (parent.getItem(name) != null) throw new Failure(Messages.Hudson_JobAlreadyExists(name)); } String mode = req.getParameter("mode"); if (mode != null && mode.equals("copy")) { String from = req.getParameter("from"); // resolve a name to Item Item src = null; if (!from.startsWith("/")) src = parent.getItem(from); if (src == null) src = Jenkins.getInstance().getItemByFullName(from); if (src == null) { if (Util.fixEmpty(from) == null) throw new Failure("Specify which job to copy"); else throw new Failure("No such job: " + from); } if (!(src instanceof TopLevelItem)) throw new Failure(from + " cannot be copied"); result = copy((TopLevelItem) src, name); } else { if (isXmlSubmission) { result = createProjectFromXML(name, req.getInputStream()); rsp.setStatus(HttpServletResponse.SC_OK); return result; } else { if (mode == null) throw new Failure("No mode given"); // create empty job and redirect to the project config screen result = createProject(Items.all().findByName(mode), name, true); } } rsp.sendRedirect2(redirectAfterCreateItem(req, result)); return result; }
public synchronized TopLevelItem createProject( TopLevelItemDescriptor type, String name, boolean notify) throws IOException { acl.checkPermission(Job.CREATE); Jenkins.getInstance().getProjectNamingStrategy().checkName(name); if (parent.getItem(name) != null) throw new IllegalArgumentException("Project of the name " + name + " already exists"); TopLevelItem item; try { item = type.newInstance(parent, name); } catch (Exception e) { throw new IllegalArgumentException(e); } try { callOnCreatedFromScratch(item); } catch (AbstractMethodError e) { // ignore this error. Must be older plugin that doesn't have this method } item.save(); add(item); Jenkins.getInstance().rebuildDependencyGraph(); if (notify) ItemListener.fireOnCreated(item); return item; }
/** * A pointless function to work around what appears to be a HotSpot problem. See JENKINS-5756 and bug 6933067 * on BugParade for more details. */ private void callOnRenamed(String newName, ItemGroup parent, String oldName) throws IOException { try { parent.onRenamed(this, oldName, newName); } catch (AbstractMethodError _) { // ignore } }
/** * Renames this item. * Not all the Items need to support this operation, but if you decide to do so, * you can use this method. */ protected void renameTo(String newName) throws IOException { // always synchronize from bigger objects first final ItemGroup parent = getParent(); synchronized (parent) { synchronized (this) { // sanity check if (newName == null) throw new IllegalArgumentException("New name is not given"); // noop? if (this.name.equals(newName)) return; Item existing = parent.getItem(newName); if (existing != null && existing!=this) // the look up is case insensitive, so we need "existing!=this" // to allow people to rename "Foo" to "foo", for example. // see http://www.nabble.com/error-on-renaming-project-tt18061629.html throw new IllegalArgumentException("Job " + newName + " already exists"); String oldName = this.name; File oldRoot = this.getRootDir(); doSetName(newName); File newRoot = this.getRootDir(); boolean success = false; try {// rename data files boolean interrupted = false; boolean renamed = false; // try to rename the job directory. // this may fail on Windows due to some other processes // accessing a file. // so retry few times before we fall back to copy. for (int retry = 0; retry < 5; retry++) { if (oldRoot.renameTo(newRoot)) { renamed = true; break; // succeeded } try { Thread.sleep(500); } catch (InterruptedException e) { // process the interruption later interrupted = true; } } if (interrupted) Thread.currentThread().interrupt(); if (!renamed) { // failed to rename. it must be that some lengthy // process is going on // to prevent a rename operation. So do a copy. Ideally // we'd like to // later delete the old copy, but we can't reliably do // so, as before the VM // shuts down there might be a new job created under the // old name. Copy cp = new Copy(); cp.setProject(new org.apache.tools.ant.Project()); cp.setTodir(newRoot); FileSet src = new FileSet(); src.setDir(oldRoot); cp.addFileset(src); cp.setOverwrite(true); cp.setPreserveLastModified(true); cp.setFailOnError(false); // keep going even if // there's an error cp.execute(); // try to delete as much as possible try { Util.deleteRecursive(oldRoot); } catch (IOException e) { // but ignore the error, since we expect that e.printStackTrace(); } } success = true; } finally { // if failed, back out the rename. if (!success) doSetName(oldName); } callOnRenamed(newName, parent, oldName); for (ItemListener l : ItemListener.all()) l.onRenamed(this, oldName, newName); } } }